Dave's Brain

Browse - Programming Tips - How can I get the current time in milliseconds?

Date: 2015sep4
OS: Linux
Language: C/C++

Q.  How can I get the current time in milliseconds?

A.  Use gettimeofday() like this:

#include <sys/time.h> long currentMillis() { struct timeval tp; gettimeofday(&tp, NULL); return tp.tv_sec * 1000 + tp.tv_usec / 1000; // Convert the seconds to milliseconds by multiplying by 1000 // Convert the microseconds to milliseconds by dividing by 1000 }
By the way:
#include <time.h> time_t now = time(NULL);
Gives you the current time in seconds.

Add a comment

Sign in to add a comment
Copyright © 2008-2017, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.