Skip to content

Instantly share code, notes, and snippets.

@jcefoli
Created Aug 28, 2014
Embed
What would you like to do?
This snippet calculates a start time, end time and total duration for your batch script
@ECHO off
SET STARTTIME=%TIME%
REM DO THINGS HERE TO TIME
timeout 5
REM DO THINGS HERE TO TIME
REM Final Calculations
SET ENDTIME=%TIME%
FOR /F "tokens=1-4 delims=:.," %%a IN ("%STARTTIME%") DO (
SET /A "start=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
FOR /F "tokens=1-4 delims=:.," %%a IN ("%ENDTIME%") DO (
SET /A "end=(((%%a*60)+1%%b %% 100)*60+1%%c %% 100)*100+1%%d %% 100"
)
REM Calculate the elapsed time by subtracting values
SET /A elapsed=end-start
REM Format the results for output
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 %hh% lss 10 SET hh=0%hh%
IF %mm% lss 10 SET mm=0%mm%
IF %ss% lss 10 SET ss=0%ss%
IF %cc% lss 10 SET cc=0%cc%
SET DURATION=%hh%:%mm%:%ss%,%cc%
ECHO Start : %STARTTIME%
ECHO Finish : %ENDTIME%
ECHO ---------------
ECHO Duration : %DURATION%
PAUSE
REM Based on http://stackoverflow.com/questions/9922498/calculate-time-difference-in-batch-file
@santhoshkptm

This comment has been minimized.

Copy link

@santhoshkptm santhoshkptm commented Jun 12, 2018

This doesn't work for PM to AM. Over night runs getting wrong duration. could you suggest?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment