Get system time accurate to milliseconds in Windows cmd

Viewed 16698

I'm trying to get the system time accurate to milliseconds in Windows cmd. I know that it's possible to get centisecond accuracy using:

echo %time%

I've found other questions that are asking the exact same thing but there is no answer that fully answers the question. Here is what I've found so far:

This solution is only good for centisecond accuracy (same as what I described above): Print time in a batch file (milliseconds)

This solution provides a timer solution but not a print current timestamp solution: Print Batch Time in Milliseconds

Any help would be much appreciated.

2 Answers

might be a workable thing for you

If you have admin privs,
and if you're running on a WINDOWS system
and if you have a networked machine configured as a time slave (to another machine),
and if want to only measure TIME DELTAS,
you can query the Windows "W32tm" utility.

It gives you the microseconds since the last Time Synchronization, via the call

C:\> w32tm /query /status /verbose

(Lotsa stuff prints out)

then pluck out only the line with the last sync time

C:\> w32tm /query /status /verbose | FIND "Time since"

Time since Last Good Sync Time: 15554.1918553s

Then, from a BAT file, do like:

for /F "tokens=7 delims= " %%a in ('w32tm /query /status /verbose ^| find "Time since" ') do set BEFORE_TIME=%%a

(what you want to measure goes here)

for /F "tokens=7 delims= " %%a in ('w32tm /query /status /verbose ^| find "Time since" ') do set AFTER_TIME=%%a

Little more work if you want to automate the subtraction, but it can be done

Related