Skip to content

Instantly share code, notes, and snippets.

@jcefoli
Created August 28, 2014 14:37
Show Gist options
  • Save jcefoli/57881d79aa4c7548324e to your computer and use it in GitHub Desktop.
Save jcefoli/57881d79aa4c7548324e to your computer and use it in GitHub Desktop.
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
@evvivame
Copy link

Hi gweber68,
Thank for your answer!

  1. Since a lot of time I use this subroutine BUT If you remove the call :TrimS the result it's same
  2. If you insert a 'pause' before and after your FOR command, You can easy verify that is this command with problem
  3. The batch of the first post run fine, the batch with your FOR coomand for AM/PM management ever worked !
    Regards

@jcefoli
Copy link
Author

jcefoli commented Mar 18, 2024

It's 2024. Why are we not using PowerShell? This is fugly

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