5

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.

Community
  • 1
  • 1
Yabasa
  • 93
  • 1
  • 1
  • 8
  • I would write a program that does it and call it from windows cmd. Are there limitations in that respect? – Neil Mar 08 '16 at 15:30
  • I'm sure there is already plenty of utilities out there that I could use but I was hoping that I was missing something more obvious. It seems like something that should be standard for most OS's. – Yabasa Mar 08 '16 at 15:44
  • MS-DOS was written a long time ago, friend. ;) There is likely more support in powershell though. – Neil Mar 08 '16 at 15:56
  • It it's not possible that's fine. I just wanted to know for sure before working on a workaround. I'm not the system admin in this case so I was looking to keep things within the capabilities of cmd. Also, I'm just curious to know for future reference. – Yabasa Mar 08 '16 at 17:05
  • No native solution exists for pure ms-dos (modern or otherwise), no. – Neil Mar 09 '16 at 07:35
  • I see. In that case if you want to officially answer I will accept it. Thanks for the info. – Yabasa Mar 09 '16 at 14:25

2 Answers2

3

As Neil pointed out there is no native solution in cmd. For anyone who has the option of using PowerShell instead, you could use the following:

(Get-Date -UFormat "%Y-%m-%d %H:%M:%S").toString() + "." + ((Get-Date).millisecond)

There may be a more succinct way of doing it but this worked for my purposes.

Since the question is tagged cmd the appropriate command line for calling this from cmd is:

powershell -command "(Get-Date -UFormat '%Y-%m-%d %H:%M:%S').toString() + '.' + ((Get-Date).millisecond)"
beppe9000
  • 1,056
  • 1
  • 13
  • 28
Yabasa
  • 93
  • 1
  • 1
  • 8
  • 1
    the question is: is it useful to get the time with milliseconds accuracy by accepting centiseconds or even seconds of delay to load powershell (or any other utility)? – Stephan Mar 14 '16 at 07:24
  • If you are loading PowerShell or some other utility every time you are recording the time then I would agree. However In my case I was only interested in recording the time during a single PowerShell session. – Yabasa Mar 14 '16 at 14:06
0

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

Stephan
  • 53,940
  • 10
  • 58
  • 91