How do I get milliseconds since midnight UTC in C?

Jake picture Jake · Jul 2, 2009 · Viewed 9.6k times · Source

The time function in time.h gives milliseconds since the epoch.

Answer

Greg Hewgill picture Greg Hewgill · Jul 2, 2009

This is a simple way:

time_t seconds_since_midnight = time(NULL) % 86400;

To get approximate milliseconds since midnight, multiply seconds_since_midnight by 1000.

If you need more resolution (consider whether you really do), you will have to use another function such as gettimeofday().