65

How do I print the time (in ms) in a Windows batch file?

I want to measure the time that passes between lines in my batch file, but Windows's "time /T" does not print milliseconds.

Assaf Lavie
  • 73,079
  • 34
  • 148
  • 203

7 Answers7

102

%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

Patrick Cuff
  • 28,540
  • 12
  • 67
  • 94
  • 7
    On my machine it shows the same time even if I sleep between calls. – Assaf Lavie Mar 03 '09 at 13:19
  • Can you post the code for the batch file that you're trying to profile? – Patrick Cuff Mar 03 '09 at 14:58
  • 11
    I doubt that :) What was the issue? Would sharing it here help others who run into the same thing? – Patrick Cuff Mar 03 '09 at 18:08
  • 3
    I have the same problem that the time is always the same during the whole execution, even if the script runs for hours, it always display the time when the script started. – MaxiWheat Dec 20 '11 at 14:13
  • %date% can be used if you want the date too. I found this looking for printing a date time (%date% %time%) and I figure others might do the same. – BlargleMonster Apr 26 '13 at 14:13
  • use this `ping localhost -n 10 >nul`. won't output any ping activity on screen. – Sorter Aug 23 '13 at 12:06
  • 1
    @MaxiWheat - see https://stackoverflow.com/questions/12524909/printing-current-date-and-time-in-dos-script which may explain your issue – yosh m Jul 26 '17 at 14:04
  • @BlargleMonster `%date%` should **not** be used to get date because it depends on locale. See [How to get Windows batch date stamp that is locale independent? (duplicate)](https://stackoverflow.com/q/56165593/995714), [Locale-unaware %DATE% and %TIME% in batch files?](https://serverfault.com/q/227345/343888) – phuclv Apr 02 '20 at 03:12
  • The answer is not very correct because the question asks for milliseconds. However, the proposal gives the time in centiseconds. (But we know, the windows time command have only the centiseconds possibility) – gotwo Jan 23 '22 at 20:58
36

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).

Joey
  • 344,408
  • 85
  • 689
  • 683
27

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,25
ENDTIME: 13:42:56,51
STARTTIME: 4937225 centiseconds
ENDTIME: 4937651 centiseconds
DURATION: 426 in centiseconds
00:00:04,26

Here is the batch script:

@echo off
setlocal

rem The format of %TIME% is HH:MM:SS,CS for example 23:59:59,99
set STARTTIME=%TIME%

rem here begins the command you want to measure
dir /s > nul
rem here ends the command you want to measure

set ENDTIME=%TIME%

rem output as time
echo STARTTIME: %STARTTIME%
echo ENDTIME: %ENDTIME%

rem convert STARTTIME and ENDTIME to centiseconds
set /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 easy
set /A DURATION=%ENDTIME%-%STARTTIME%

rem we might have measured the time inbetween days
if %ENDTIME% LSS %STARTTIME% set set /A DURATION=%STARTTIME%-%ENDTIME%

rem now break the centiseconds down to hors, minutes, seconds and the remaining centiseconds
set /A DURATIONH=%DURATION% / 360000
set /A DURATIONM=(%DURATION% - %DURATIONH%*360000) / 6000
set /A DURATIONS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000) / 100
set /A DURATIONHS=(%DURATION% - %DURATIONH%*360000 - %DURATIONM%*6000 - %DURATIONS%*100)

rem some formatting
if %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 outputing
echo STARTTIME: %STARTTIME% centiseconds
echo ENDTIME: %ENDTIME% centiseconds
echo DURATION: %DURATION% in centiseconds
echo %DURATIONH%:%DURATIONM%:%DURATIONS%,%DURATIONHS%

endlocal
goto :EOF
nusi
  • 1,016
  • 9
  • 12
  • 2
    %TIME% is in the format H:MM:SS,CS after midnight and hence conversion to centiseconds doesn't work. Seeing Patrick Cuff's post with 6:46am it seems that it is not only me. – Jaroslaw Pawlak Apr 01 '13 at 23:18
  • the (1%STARTTIME:~6,2%-100)*100 should be (1%STARTTIME:~6,2%-100)*1000 etc. – Yan Jun 08 '14 at 13:31
  • the (1%STARTTIME:~9,2%-100) should be (1%STARTTIME:~9,2%-100) * 10 etc. – Yan Jun 08 '14 at 13:33
  • 1
    @jaroslaw-pawlak, if your %TIME% format is H:MM:SS.CS (hour < 10 begins with a space instead of a 0), for the script to work, you have to use `set STARTTIME=%TIME: =0%` and `set ENDTIME=%TIME: =0%`. – Matt Roy Aug 29 '18 at 15:40
3

To time task in CMD is as simple as

echo %TIME% && your_command && cmd /v:on /c echo !TIME!
igor
  • 5,351
  • 2
  • 26
  • 22
  • 2
    yes, but the problem is, starting a new process costs time and leads to a wrong value (`try `echo %TIME% & cmd /v:on /c echo !TIME!` it's about 20ms on my system) Better create the process outside the measurement: (`cmd /v:on /c "echo !time! & echo !time!"` gives zero delay [technically there has to be a delay, but it's below display accuracy]) or in above case: `cmd /v:on /c "echo !time! & your_command & echo !time!"` – Stephan Oct 10 '19 at 12:58
  • 3
    (Note: a single `&` is sufficient. It means "and then do". `&&` means "and if successful, do") – Stephan Oct 10 '19 at 12:59
3

Maybe this tool (archived version ) could help? It doesn't return the time, but it is a good tool to measure the time a command takes.

beppe9000
  • 1,056
  • 1
  • 13
  • 28
Stefan
  • 43,293
  • 10
  • 75
  • 117
  • Thanks, +1 helpful. But still interesting why there's no simple time print in batch... – Assaf Lavie Mar 03 '09 at 08:36
  • 4
    I like this suggestion. Unfortunately without the name of the tool, when the link breaks (e.g. now) I can't see what you're talking about. – Stephen Feb 14 '13 at 05:16
  • 4
    I _think_ it might've been `timethis.exe` (which, at the time of writing, seems to be available via [this link](http://download.microsoft.com/download/win2000platform/timethis/1.00.0.1/NT5/EN-US/timethis_setup.exe). – Henning Mar 06 '13 at 02:41
  • Broken link but it's in the Internet archive: [timethis_setup.exe](https://web.archive.org/web/20140830232632if_/http://download.microsoft.com/download/win2000platform/timethis/1.00.0.1/nt5/en-us/timethis_setup.exe). – cdlvcdlv Jul 06 '18 at 10:03
2

%TIME% is in the format H:MM:SS,CS after midnight and hence conversion to centiseconds >doesn't work. Seeing Patrick Cuff's post with 6:46am it seems that it is not only me.

But with this lines bevor you should will fix that problem easy:

if " "=="%StartZeit:~0,1%" set StartZeit=0%StartZeit:~-10%
if " "=="%EndZeit:~0,1%" set EndZeit=0%EndZeit:~-10%

Thanks for your nice inspiration! I like to use it in my mplayer, ffmpeg, sox Scripts to pimp my mediafiles for old PocketPlayers just for fun.

Mae
  • 21
  • 1
2

You have to be careful with what you want to do, because it is not just about to get the time.

The batch has internal variables to represent the date and the tme: %DATE% %TIME%. But they dependent on the Windows Locale.

%Date%:

  • dd.MM.yyyy
  • dd.MM.yy
  • d.M.yy
  • dd/MM/yy
  • yyyy-MM-dd

%TIME%:

  • H:mm:ss,msec
  • HH:mm:ss,msec

Now, how long your script will work and when? For example, if it will be longer than a day and does pass the midnight it will definitely goes wrong, because difference between 2 timestamps between a midnight is a negative value! You need the date to find out correct distance between days, but how you do that if the date format is not a constant? Things with %DATE% and %TIME% might goes worser and worser if you continue to use them for the math purposes.

The reason is the %DATE% and %TIME% are exist is only to show a date and a time to user in the output, not to use them for calculations. So if you want to make correct distance between some time values or generate some unique value dependent on date and time then you have to use something different and accurate than %DATE% and %TIME%.

I am using the wmic windows builtin utility to request such things (put it in a script file):

for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE`) do if "%%i" == "LocalDateTime" echo.%%j

or type it in the cmd.exe console:

for /F "usebackq tokens=1,2 delims==" %i in (`wmic os get LocalDateTime /VALUE`) do @if "%i" == "LocalDateTime" echo.%j

The disadvantage of this is a slow performance in case of frequent calls. On mine machine it is about 12 calls per second.

If you want to continue use this then you can write something like this (get_datetime.bat):

@echo off

rem Description:
rem   Independent to Windows locale date/time request.

rem Drop last error level
cd .

rem drop return value
set "RETURN_VALUE="

for /F "usebackq tokens=1,2 delims==" %%i in (`wmic os get LocalDateTime /VALUE 2^>NUL`) do if "%%i" == "LocalDateTime" set "RETURN_VALUE=%%j"

if not "%RETURN_VALUE%" == "" (
  set "RETURN_VALUE=%RETURN_VALUE:~0,18%"
  exit /b 0
)

exit /b 1

Now, you can parse %RETURN_VALUE% somethere in your script:

call get_datetime.bat
set "FILE_SUFFIX=%RETURN_VALUE:.=_%"
set "FILE_SUFFIX=%FILE_SUFFIX:~8,2%_%FILE_SUFFIX:~10,2%_%FILE_SUFFIX:~12,6%"
echo.%FILE_SUFFIX%
Andry
  • 2,273
  • 29
  • 28