Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Calculate time difference in Windows batch file

How can I get the difference between two times in a Batch file? Because I want to print it in an HTML file.

I thought this would be possible, but it isn't.

Set "tijd=%time%" echo %tijd% echo %time%-%tijd% 

Results:

11:07:48,85 11:16:58,99-11:07:48,85 

But what I want is:

00:09:10,14 

Or 9 minutes and 10 seconds or 550 seconds

like image 829
Danny Gloudemans Avatar asked Mar 29 '12 09:03

Danny Gloudemans


People also ask

How do I measure execution time of a command on the Windows command line?

Simpler: echo %time% & *mycommand* & call echo %^time% . See also this answer. If your command outputs too much, maybe it's easier just put the 2 time together: cmd /v:on /c "mycommand & echo begin=%time% endtime=! time!"

What is %% in a batch file?

Use double percent signs ( %% ) to carry out the for command within a batch file. Variables are case sensitive, and they must be represented with an alphabetical value such as %a, %b, or %c. ( <set> ) Required. Specifies one or more files, directories, or text strings, or a range of values on which to run the command.

What does %1 do in batch?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.

What is == in batch?

[ == ] (Double Equals) The "IF" command uses this to test if two strings are equal: IF "%1" == "" GOTO HELP. means that if the first parameter on the command line after the batch file name is equal to nothing, that is, if a first parameter is not given, the batch file is to go to the HELP label.


2 Answers

@echo off  rem Get start time: for /F "tokens=1-4 delims=:.," %%a in ("%time%") do (    set /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100" )  rem Any process here...  rem Get end time: for /F "tokens=1-4 delims=:.," %%a in ("%time%") do (    set /A "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100" )  rem Get elapsed time: set /A elapsed=end-start  rem Show elapsed time: set /A hh=elapsed/(60*60*100), rest=elapsed%%(60*60*100), mm=rest/(60*100), rest%%=60*100, ss=rest/100, cc=rest%%100 if %mm% lss 10 set mm=0%mm% if %ss% lss 10 set ss=0%ss% if %cc% lss 10 set cc=0%cc% echo %hh%:%mm%:%ss%,%cc% 

EDIT 2017-05-09: Shorter method added

I developed a shorter method to get the same result, so I couldn't resist to post it here. The two for commands used to separate time parts and the three if commands used to insert leading zeros in the result are replaced by two long arithmetic expressions, that could even be combined into a single longer line.

The method consists in directly convert a variable with a time in "HH:MM:SS.CC" format into the formula needed to convert the time to centiseconds, accordingly to the mapping scheme given below:

       HH        :      MM        :      SS        .       CC  (((10  HH  %%100)*60+1  MM  %%100)*60+1  SS  %%100)*100+1  CC  %%100 

That is, insert (((10 at beginning, replace the colons by %%100)*60+1, replace the point by %%100)*100+1 and insert %%100 at end; finally, evaluate the resulting string as an arithmetic expression. In the time variable there are two different substrings that needs to be replaced, so the conversion must be completed in two lines. To get an elapsed time, use (endTime)-(startTime) expression and replace both time strings in the same line.

EDIT 2017/06/14: Locale independent adjustment added

EDIT 2020/06/05: Pass-over-midnight adjustment added

@echo off setlocal EnableDelayedExpansion  set "startTime=%time: =0%"  set /P "=Any process here..."  set "endTime=%time: =0%"    rem Get elapsed time: set "end=!endTime:%time:~8,1%=%%100)*100+1!"  &  set "start=!startTime:%time:~8,1%=%%100)*100+1!" set /A "elap=((((10!end:%time:~2,1%=%%100)*60+1!%%100)-((((10!start:%time:~2,1%=%%100)*60+1!%%100), elap-=(elap>>31)*24*60*60*100"  rem Convert elapsed time to HH:MM:SS:CC format: set /A "cc=elap%%100+100,elap/=100,ss=elap%%60+100,elap/=60,mm=elap%%60+100,hh=elap/60+100"  echo Start:    %startTime% echo End:      %endTime% echo Elapsed:  %hh:~1%%time:~2,1%%mm:~1%%time:~2,1%%ss:~1%%time:~8,1%%cc:~1% 

You may review a detailed explanation of this method at this answer.

like image 152
Aacini Avatar answered Oct 02 '22 21:10

Aacini


As answered here: How can I use a Windows batch file to measure the performance of console application?

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 
like image 24
Danny Gloudemans Avatar answered Oct 02 '22 20:10

Danny Gloudemans