Calculate Time Difference in Windows Batch File
Calculate Time Difference in Windows Batch File
Make your voice heard. Take the 2019 Developer Survey now
Set "tijd=%time%"
17 echo %tijd%
echo %time%-%tijd%
Results:
11:07:48,85
11:16:58,99-11:07:48,85
00:09:10,14
8 Answers
@echo off
https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file 2/18
1/29/2019 Calculate time difference in Windows batch file - Stack Overflow
HH : MM :
@echo off
setlocal EnableDelayedExpansion
https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file 3/18
1/29/2019 Calculate time difference in Windows batch file - Stack Overflow
echo End: %endTime%
echo Elapsed: %hh:~1%%time:~2,1%%mm
https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file 4/18
1/29/2019 Calculate time difference in Windows batch file - Stack Overflow
of the used commands is only
centiseconds.
STARTTIME: 13:42:52,25
ENDTIME: 13:42:56,51
STARTTIME: 4937225 centiseconds
ENDTIME: 4937651 centiseconds
DURATION: 426 in centiseconds
00:00:04,26
@echo off
setlocal
set ENDTIME=%TIME%
rem outputing
echo STARTTIME: %STARTTIME% centise
echo ENDTIME: %ENDTIME% centisecond
echo DURATION: %DURATION% in centis
echo %DURATIONH%:%DURATIONM%:%DURAT
endlocal
goto :EOF
https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file 5/18
1/29/2019 Calculate time difference in Windows batch file - Stack Overflow
Gynnad
1,272 3 30 49
https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file 6/18
1/29/2019 Calculate time difference in Windows batch file - Stack Overflow
rem ****************** MAIN COD
set STARTTIME=%TIME%
set ENDTIME=%TIME%
set DURATION=%hh%:%mm%:%ss%,%cc%
Output:
Start : 11:02:45.92
Finish : 11:02:48.98
---------------
Duration : 00:00:03,06
@echo off
setlocal
set starttime=%TIME%
echo Start Time: %starttime%
REM --------------------------------
REM --- PUT THE CODE YOU WANT TO MEA
REM --------------------------------
set endtime=%TIME%
echo End Time: %endtime%
call :elapsed_time %starttime% %endt
echo Duration: %duration%
endlocal
echo on & goto :eof
:time_to_centiseconds
:: %~1 - time
:: %~2 - centiseconds output variabl
setlocal
set _time=%~1
for /F "tokens=1-4 delims=:.," %%a i
set /A "_result=(((%%a*60)+1%%b %
)
endlocal & set %~2=%_result%
goto :eof
:centiseconds_to_time
:: %~1 - centiseconds
:: %~2 - time output variable
setlocal
set _centiseconds=%~1
rem now break the centiseconds down
centiseconds
set /A _h=%_centiseconds% / 360000
set /A _m=(%_centiseconds% - %_h%*36
set /A _s=(%_centiseconds% - %_h%*36
set /A _hs=(%_centiseconds% - %_h%*3
rem some formatting
if %_h% LSS 10 set _h=0%_h%
if %_m% LSS 10 set _m=0%_m%
if %_s% LSS 10 set _s=0%_s%
By using our site,if %_hs%
you LSS 10 that
acknowledge set you
_hs=0%_hs%
have read and understand our , , and our
set _result=%_h%:%_m%:%_s%.%_hs%
. endlocal & set %~2=%_result%
https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file 8/18
1/29/2019 Calculate time difference in Windows batch file - Stack Overflow
goto :eof
:elapsed_time
:: %~1 - time1 - start time
:: %~2 - time2 - end time
:: %~3 - elapsed time output
setlocal
set _time1=%~1
set _time2=%~2
call :time_to_centiseconds %_time1%
call :time_to_centiseconds %_time2%
set /A _duration=%_centi2%-%_centi1%
call :centiseconds_to_time %_duratio
endlocal & set %~3=%_result%
goto :eof
@ECHO OFF
:: F U N C T I O N S
:__START_TIME_MEASURE
SET STARTTIME=%TIME%
SET STARTTIME=%STARTTIME: =0%
EXIT /B 0
:__STOP_TIME_MEASURE
SET ENDTIME=%TIME%
SET ENDTIME=%ENDTIME: =0%
SET /A STARTTIME=(1%STARTTIME:~0,2%-
(1%STARTTIME:~6,2%-100)*100 + (1%STA
SET /A ENDTIME=(1%ENDTIME:~0,2%-100)
By using our site,(1%ENDTIME:~6,2%-100)*100
you acknowledge that you have+ read and understand our
(1%ENDTI , , and our
. SET /A DURATION=%ENDTIME%-%STARTTIME
IF %DURATION% == 0 SET TIMEDIFF=00:0
https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file 9/18
1/29/2019 Calculate time difference in Windows batch file - Stack Overflow
IF %ENDTIME% LSS %STARTTIME% SET /A
SET /A DURATIONH=%DURATION% / 360000
SET /A DURATIONM=(%DURATION% - %DURA
SET /A DURATIONS=(%DURATION% - %DURA
SET /A DURATIONHS=(%DURATION% - %DUR
%DURATIONS%*100)
IF %DURATIONH% LSS 10 SET DURATIONH=
IF %DURATIONM% LSS 10 SET DURATIONM=
IF %DURATIONS% LSS 10 SET DURATIONS=
IF %DURATIONHS% LSS 10 SET DURATIONH
SET TIMEDIFF=%DURATIONH%:%DURATIONM%
EXIT /B 0
:: U S A G E
:: Start Measuring
CALL :__START_TIME_MEASURE
:: Stop Measuring
CALL :__STOP_TIME_MEASURE
:: Possible Result
:: Execute Job... [Done] (00:02:12,3
:: Between 'Execute Job... ' and '[D
:: timediff
By using our site,::
you acknowledge
Input that you
and output have is
format read andsaunderstand our
the , , and our
. :: If EndTime is less than StartTime
:: EndTime will be treated as a ti
https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file 10/18
1/29/2019 Calculate time difference in Windows batch file - Stack Overflow
:: in that case, function measures
24 hours minus 1 centisecond
:: time elements can have values g
12:247:853.5214
:: provided than the total represe
:: otherwise the result will not b
:: If EndTime is greater than or equ
:: No formal limitation applies to
:: except that total represented t
Example:
@echo off
setlocal EnableExtensions
set "TIME="
set "Start=%TIME%"
REM Do some stuff here...
set "End=%TIME%"
pause
exit /b
It takes
By using our site, 3 parameters
you acknowledge that from left read
you have to right:
and understand our , , and our
.
https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file 11/18
1/29/2019 Calculate time difference in Windows batch file - Stack Overflow
Param1: Name of the environment
variable to save the result to.
Param2: Name of the environment
variable to be passed to the function
containing StartTime string
Param3: Name of the environment
variable to be passed to the function
containing EndTime string
@echo off
setlocal EnableExtensions
set "start=23:57:33,12"
set "end=00:02:19,41"
pause
exit /b
By using our site, you acknowledge that you have read and understand our , , and our
:: put the :timediff function here
.
https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file 12/18
1/29/2019 Calculate time difference in Windows batch file - Stack Overflow
Output:
Difference: 00:04:46,29
@echo off
setlocal EnableExtensions
set "start=00:00:00.00"
set "end=27:2457:433.85935"
echo,
echo %end% is equivalent to %normali
echo,
pause
exit /b
Output:
27:2457:433.85935 is equivalent to 6
@echo off
setlocal EnableExtensions
set "start=00:00:00.00"
set "end=00:00:00.2147483647"
echo,
echo 2147483647 centiseconds equals
echo,
pause
exit /b
Output:
https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file 13/18
1/29/2019 Calculate time difference in Windows batch file - Stack Overflow
@ECHO OFF
ECHO.
ECHO DEMO timer function
ECHO --------------------
SET DELAY=4
CALL:timer elapsed_time
ECHO.
PAUSE
SET _t=%time%
SET _t=%_t::0=: %
SET _t=%_t:,0=, %
SET _t=%_t:.0=. %
SET _t=%_t:~0,2% * 360000 + %_t
SET /A _t=%_t%
https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file 14/18
1/29/2019 Calculate time difference in Windows batch file - Stack Overflow
SET _f=%~2
IF DEFINED _f (
IF "%_f%" == "s" (
) ELSE (
IF "%_f%" == "m" (
SET /A "_t=%_t% / 60
) ELSE (
IF "%_f%" == "h" (
SET /A "_t=%_t%
) ELSE (
SET /A "_h=%_t%
SET /A "_m=(%_t%
SET /A "_s=(%_t%
SET /A "_cs=(%_t
IF !_h! LSS 10 S
IF !_m! LSS 10 S
IF !_s! LSS 10 S
IF !_cs! LSS 10
SET "_t=!_h!:!_m
SET "_t=!_t:00:=
)
)
)
)
by Default : 9404
in Seconds : 94
Formatted : 01:34.05 (HH:MM:SS.CS)
@echo off
setlocal EnableDelayedExpansion
https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file 16/18
1/29/2019 Calculate time difference in Windows batch file - Stack Overflow
Usage:
timer & echo start_cmds & timeout /t
3 & echo end_cmds & timer
timer & timer "23:23:23,00"
timer "23:23:23,00" & timer
timer "13.23.23,00" & timer
"03:03:03.00"
timer & timer "0:00:00.00" no & cmd
/v:on /c echo until
midnight=!timer_end!
Input can now be mixed, for those
unlikely, but possible time format
changes during execution
@echo off
set "TIMER=call :timer" & rem short
echo.
echo EXAMPLE:
call :timer
timeout /t 3 >nul & rem Any process
call :timer
echo.
echo SHORT MACRO:
%TIMER% & timeout /t 1 & %TIMER%
echo.
echo TEST INPUT:
set "start=22:04:04.58"
set "end=04.22.44,22"
echo %start% ~ start & echo %end% ~
call :timer "%start%"
call :timer "%end%"
echo.
%TIMER% & %TIMER% "00:00:00.00" no
echo UNTIL MIDNIGHT: %timer_end%
echo.
pause
exit /b
https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file 17/18
1/29/2019 Calculate time difference in Windows batch file - Stack Overflow
for /f "tokens=1-6 delims=0123456789
CE=%%i&set DE=%%k&set CS=%%l&set DS=
set "TE=!timer_end:%DE%=%%100)*100+1
set/A "T=((((10!TE:%CE%=%%100)*60+1
set/A "T=!T:-=8640000-!"
set/A "cc=T%%100+100,T/=100,ss=T%%60
set "value=!hh:~1!%CE%!mm:~1!%CE%!ss
endlocal & set "timer_end=%value%" &
By using our site, you acknowledge that you have read and understand our , , and our
.
https://stackoverflow.com/questions/9922498/calculate-time-difference-in-windows-batch-file 18/18