Print time in a batch file (milliseconds) Print time in a batch file (milliseconds) windows windows

Print time in a batch file (milliseconds)


%time% should work, provided enough time has elapsed between calls:

@echo OFF@echo %time%ping -n 1 -w 1 127.0.0.1 1>nul@echo %time%

On my system I get the following output:

6:46:13.50
6:46:13.60


If you're doing something like

for /l %%i in (1,1,500) do @echo %time%

or

if foo (    echo %time%    do_something    echo %time%)

then you could simply put a setlocal enabledelayedexpansion at the beginning of your batch file and use !time! instead of %time% which gets evaluated on execution, not on parsing the line (which includes complete blocks enclosed in parentheses).


Below batch "program" should do what you want. Please note that it outputs the data in centiseconds instead of milliseconds. The precision of the used commands is only centiseconds.

Here is an example output:

STARTTIME: 13:42:52,25ENDTIME: 13:42:56,51STARTTIME: 4937225 centisecondsENDTIME: 4937651 centisecondsDURATION: 426 in centiseconds00:00:04,26

Here is the batch script:

@echo offsetlocalrem The format of %TIME% is HH:MM:SS,CS for example 23:59:59,99set STARTTIME=%TIME%rem here begins the command you want to measuredir /s > nulrem here ends the command you want to measureset ENDTIME=%TIME%rem output as timeecho STARTTIME: %STARTTIME%echo ENDTIME: %ENDTIME%rem convert STARTTIME and ENDTIME to centisecondsset /A STARTTIME=(1%STARTTIME:~0,2%-100)*360000 + (1%STARTTIME:~3,2%-100)*6000 + (1%STARTTIME:~6,2%-100)*100 + (1%STARTTIME:~9,2%-100)set /A ENDTIME=(1%ENDTIME:~0,2%-100)*360000 + (1%ENDTIME:~3,2%-100)*6000 + (1%ENDTIME:~6,2%-100)*100 + (1%ENDTIME:~9,2%-100)rem calculating the duratyion is easyset /A DURATION=%ENDTIME%-%STARTTIME%rem we might have measured the time inbetween daysif %ENDTIME% LSS %STARTTIME% set set /A DURATION=%STARTTIME%-%ENDTIME%rem now break the centiseconds down to hors, minutes, seconds and the remaining centisecondsset /A DURATIONH=%DURATION% / 360000set /A DURATIONM=(%DURATION% - %DURATIONH%*360000) / 6000set /A DURATIONS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000) / 100set /A DURATIONHS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000 - %DURATIONS%*100)rem some formattingif %DURATIONH% LSS 10 set DURATIONH=0%DURATIONH%if %DURATIONM% LSS 10 set DURATIONM=0%DURATIONM%if %DURATIONS% LSS 10 set DURATIONS=0%DURATIONS%if %DURATIONHS% LSS 10 set DURATIONHS=0%DURATIONHS%rem outputingecho STARTTIME: %STARTTIME% centisecondsecho ENDTIME: %ENDTIME% centisecondsecho DURATION: %DURATION% in centisecondsecho %DURATIONH%:%DURATIONM%:%DURATIONS%,%DURATIONHS%endlocalgoto :EOF