0

I am aware that one can print batch time up to the centisecond with @echo %time%. Is there a way to get milliseconds with precision of 3 and leading zero in hrs as well? E.g 06:58:30.483

%time% prints without leading zero in hrs and with 2 fraction in ms E.g 6:58:25.25

  • 4
    There is nothing built into the batch language for this. (Maybe you can find a program that will give you a more accurate time and call it from your batch file.) – Raymond Chen Mar 18 '22 at 16:33
  • 1
    There can be used in a batch file `%SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime` to get the current local date/time with milliseconds. Examples using that command can be found easily with a Stack Overflow search using `[batch-file] wmic localdatetime` as it can be seen on the [results](https://stackoverflow.com/search?q=%5Bbatch-file%5D+wmic+localdatetime) of this search. I explained the usage of this command in full details in my answer on [Time is set incorrectly after midnight](https://stackoverflow.com/a/60126994/3074564). – Mofi Mar 18 '22 at 16:33
  • 1
    You should be aware that the execution of `wmic.exe` takes several milliseconds, 60 ms on my PC running `for /L %I in (1,1,10) do %SystemRoot%\System32\wbem\wmic.exe OS GET LocalDateTime` in a command prompt window. So I do not really understand what you expect from a batch file processed by `cmd.exe` regarding to a time with a resolution of one millisecond, but an accuracy of dozens of milliseconds due to the time required to open batch file, read a line, parse it, close the batch file and execute the command. – Mofi Mar 18 '22 at 16:48
  • Use PowerShell instead: [Find time in milliseconds using PowerShell?](https://stackoverflow.com/q/9060269/995714) – phuclv Mar 18 '22 at 16:54
  • 1
    if you're just interested in the format, `echo %time: =0%%random:~-1%` is the most accurate thing you will get without using `wmic`, powershell, or external tools (all of which won't be more accurate due to their loading time). – Stephan Mar 18 '22 at 17:55
  • @Stepan Yes, `@setlocal EnableExtensions EnableDelayedExpansion & (for /L %%I in (1,1,10) do @echo !time: =0!!random:~-1!) & endlocal` in a batch file is definitely the fastest solution in comparison to the others provided in the comments and answers. But the last digit has a random value and the time format is on my PC with a comma instead of a decimal point. It is additionally possible that output is first `21:06:27,956` and second `21:06:27,954` and third `21:06:27,957`. Appending a randomized third digit to the two digit fractional seconds value is not so good on fast running the ECHOs. – Mofi Mar 18 '22 at 20:11
  • @Stephan yes the process spawning time is significant, so the better solution is to scrap the legacy cmd completely and only use powershell – phuclv Mar 19 '22 at 01:15
  • @phuclv: ... or (nearly) *any* other language but batch... `:D` – Stephan Mar 19 '22 at 06:54

2 Answers2

1

Because I'd assume it is much faster than or , here's a with a helper:

<!-- ::Script Language="Batch"
@For /F %%G In ('%SystemRoot%\System32\cscript.exe //NoLogo "%~f0?.wsf"')Do @Echo(%%G
@Pause&Exit /B
-->
<Job><Script Language="VBScript">
        t0 = Timer : t1 = Int(t0) : ms = Int((t0 - t1) * 1000) : s = t1 mod 60
        t1 = Int(t1 / 60) : m = t1 mod 60 : h = Int(t1 / 60)
        strT = String(2 - Len(h), "0") & h & ":"
        strT = strT & String(2 - Len(m), "0") & m & ":"
        strT = strT & String(2 - Len(s), "0") & s & "."
        strT = strT & String(3 - Len(ms), "0") & ms
        WScript.Echo strT</Script></Job>
Compo
  • 36,585
  • 5
  • 27
  • 39
0

If you are on a supported windows system, you already have a program that will produce what you want using a batch-file running under cmd.

FOR /F "delims=" %%A IN ('powershell.exe -NoLogo -NoProfile -Command "Get-Date -Format HH:mm:ss.fff"') DO (SET "THETIME=%%~A")
ECHO THETIME is %THETIME%
lit
  • 14,456
  • 10
  • 65
  • 119