问题
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.
回答1:
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)"
回答2:
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
来源:https://stackoverflow.com/questions/35871328/get-system-time-accurate-to-milliseconds-in-windows-cmd