How can you obtain the system clock's current time of day (in milliseconds) in C++? This is a windows specific app.
7 Answers
The easiest (and most direct) way is to call GetSystemTimeAsFileTime()
, which returns a FILETIME
, a struct which stores the 64-bit number of 100-nanosecond intervals since midnight Jan 1, 1601.
At least at the time of Windows NT 3.1, 3.51, and 4.01, the GetSystemTimeAsFileTime()
API was the fastest user-mode API able to retrieve the current time. It also offers the advantage (compared with GetSystemTime() -> SystemTimeToFileTime()) of being a single API call, that under normal circumstances cannot fail.
To convert a FILETIME ft_now;
to a 64-bit integer named ll_now
, use the following:
ll_now = (LONGLONG)ft_now.dwLowDateTime + ((LONGLONG)(ft_now.dwHighDateTime) << 32LL);
You can then divide by the number of 100-nanosecond intervals in a millisecond (10,000 of those) and you have milliseconds since the Win32 epoch.
To convert to the Unix epoch, subtract 116444736000000000LL
to reach Jan 1, 1970.
You mentioned a desire to find the number of milliseconds into the current day. Because the Win32 epoch begins at a midnight, the number of milliseconds passed so far today can be calculated from the filetime with a modulus operation. Specifically, because there are 24 hours/day * 60 minutes/hour * 60 seconds/minute * 1000 milliseconds/second = 86,400,000 milliseconds/day
, you could user the modulus of the system time in milliseconds modulus 86400000LL
.
For a different application, one might not want to use the modulus. Especially if one is calculating elapsed times, one might have difficulties due to wrap-around at midnight. These difficulties are solvable, the best example I am aware is Linus Torvald's line in the Linux kernel which handles counter wrap around.
Keep in mind that the system time is returned as a UTC time (both in the case of GetSystemTimeAsFileTime()
and simply GetSystemTime()
). If you require the local time as configured by the Administrator, then you could use GetLocalTime()
.

- 18,667
- 3
- 39
- 62
-
3+1 for indepth breakdown. Care to add the bit for NT5.1+ regarding 'at least at the time of Windows NT 3.1, 3.51, and 4.01.. fastest user-api'. Does it imply it is no longer the case? – rama-jka toti Nov 08 '09 at 12:49
-
1It only implies that I can't swear to the situation post-4.01, but in fact I believe this API is still the fastest. In the old days, it was implemented as a couple of MOV operations -- the SystemTimeAsFileTime is stored in a shared memory page which all processes may read. – Heath Hunnicutt Nov 08 '09 at 17:14
-
1Very nice, in depth answer. To answer your last question, I am simulating an environment scenario, where as the day progresses, the environment grows darker (as the outdoors would). It's just a matter of getting the time of day (in milliseconds since midnight is the format my engine currently reads) for syncing up :) – Mark Nov 09 '09 at 04:14
-
2You can also use the fact that FILETIME is laid out like a long long would be and just cast it. Look at how LARGE_INTEGER works with the union, it's the same idea. `*reinterpret_cast
(&ft_now)` – Matt Price Mar 10 '10 at 21:31 -
3Best answer on the internet – notbad.jpeg Apr 07 '14 at 02:10
-
2Should be "To convert file time (in 100-nanosecond) to the Unix epoch, subtract 116444736000000000LL to reach Jan 1, 1970." though – Kaifei Jun 06 '14 at 01:03
-
3@MattPrice: you CAN'T just cast, see https://msdn.microsoft.com/en-gb/library/windows/desktop/ms724284(v=vs.85).aspx: "Do not cast a pointer to a FILETIME structure to either a ULARGE_INTEGER* or __int64* value because it can cause alignment faults on 64-bit Windows" – Saul Feb 17 '15 at 15:36
-
@Kaifei - thanks for pointing that out. I replaced "add" in my answer with "subtract," because, you're correct, that's what I meant. – Heath Hunnicutt Mar 15 '16 at 22:29
To get the time expressed as UTC, use GetSystemTime
in the Win32 API.
SYSTEMTIME st;
GetSystemTime(&st);
SYSTEMTIME
is documented as having these relevant members:
WORD wYear;
WORD wMonth;
WORD wDayOfWeek;
WORD wDay;
WORD wHour;
WORD wMinute;
WORD wSecond;
WORD wMilliseconds;
As shf301 helpfully points out below, GetLocalTime
(with the same prototype) will yield a time corrected to the user's current timezone.
You have a few good answers here, depending on what you're after. If you're looking for just time of day, my answer is the best approach -- if you need solid dates for arithmetic, consider Alex's. There's a lot of ways to skin the time cat on Windows, and some of them are more accurate than others (and nobody has mentioned QueryPerformanceCounter
yet).

- 15,584
- 8
- 52
- 59
-
6Use GetLocalTime to get the time and date as the user would see. http://msdn.microsoft.com/en-us/library/ms724338%28VS.85%29.aspx – shf301 Nov 08 '09 at 03:43
-
2QueryPerformanceCounter... I think you might be joking, since it has a lot of drawbacks that would make it a poor selection here. – Heath Hunnicutt Nov 08 '09 at 08:00
-
1and once again , one got to search for the correct corresponding include...pffff – Phil Mar 11 '18 at 15:37
A cut-to-the-chase example of Jed's answer above:
const std::string currentDateTime() {
SYSTEMTIME st, lt;
GetSystemTime(&st);
char currentTime[84] = "";
sprintf(currentTime,"%d/%d/%d %d:%d:%d %d",st.wDay,st.wMonth,st.wYear, st.wHour, st.wMinute, st.wSecond , st.wMilliseconds);
return string(currentTime); }

- 6,649
- 1
- 50
- 52
-
1`sprintf(currentTime,"%.4d%.2d%.2d%.2d%.2d%.2d%.4d",st.wYear,st.wMonth,st.wDay, st.wHour, st.wMinute, st.wSecond , st.wMilliseconds);` for a fixed length string – Antonio Mar 10 '17 at 15:51
-
1This gets the UTC time; `GetLocalTime()` works similarly but gives the local timezone time – M.M Feb 08 '18 at 12:04
Use GetSystemTime, first; then, if you need that, you can call SystemTimeToFileTime on the SYSTEMTIME
structure that the former fills for you. A FILETIME
is a 64-bit count of 100-nanosecs intervals since an epoch, and so more suitable for arithmetic; a SYSTEMTIME
is a structure with all the expected fields (year, month, day, hour, etc, down to milliseconds). If you want to know "how many milliseconds have elapsed since midnight", for example, subtracting two FILETIME
structures (one for the current time, one obtained by converting the same SYSTEMTIME
after zeroing out the appropriate fields) and dividing by the appropriate power of ten is probably the simplest available approach.

- 854,459
- 170
- 1,222
- 1,395
-
Jed, right, writing from memory I thought there was an underscore -- editing now to fix, tx. – Alex Martelli Nov 08 '09 at 03:43
Depending on the needs of your application there are six common options. This Dr Dobbs Journal article will give you all the information (and more) you need on choosing the best one.
In your specific case, from this article:
GetSystemTime() retrieves the current system time and instantiates a SYSTEMTIME structure, which is composed of a number of separate fields including year, month, day, hours, minutes, seconds, and milliseconds.

- 60,973
- 31
- 151
- 169
-
1Correct me if I'm wrong, but doesn't GetTickCount give hte number of milliseconds that have passed since oyur application launch? – Mark Nov 08 '09 at 03:40
-
1
-
1@Mark: Since the system was booted. `GetTickCount64`, since Windows Vista, expands that to 64-bit; the former is limited to 32. – Jed Smith Nov 08 '09 at 03:41
-
3Actually, It's from system boot I believe, but you are still right in that it was the weong function. – Ash Nov 08 '09 at 03:42
-
Depending on what you are trying to do GetTickCount might be what you want, though, since it just returns a simple DWORD of the number of ms past since boot. – Al Ro Dec 01 '21 at 22:20
While it's not what the question asks, it's worth considering why you want this info.
If all you want to do is keep track of how long something takes to calculate or the time past since the last user interaction, consider using the uptime (milliseconds since boot), which is much simpler to get: GetTickCount() or GetTickCount64(). This is all I wanted to do but I went down the epoch rabbit hole first because that's how you do it under unix.

- 466
- 2
- 11
Here is some code that works in Windows which I've used in a Open Watcom C project. It should work in C++ It returns seconds (not milliseconds) using _dos_gettime or gettime
double seconds(void)
{
#ifdef __WATCOMC__
struct dostime_t t;
_dos_gettime(&t);
return ((double)t.hour * 3600 + (double)t.minute * 60 + (double)t.second + (double)t.hsecond * 0.01);
#else
struct time t;
gettime(&t);
return ((double)t.ti_hour * 3600 + (double)t.ti_min * 60 + (double)t.ti_sec + (double)t.ti_hund * 0.01);
#endif
}

- 193
- 1
- 8