Calculate two times in a batch variable

Asked

Viewed 1,234 times

1

Is it possible to calculate the difference between two times with the batch and use the answer as a variable within the command? The "time" command allows you to make the calculations between start and end time.

set STARTTIME=%TIME%
set ENDTIME=%TIME%
set /A DURATION=%ENDTIME%-%STARTTIME%

The response variable (DURATION) returns as hour:minute:second:millisecond, can I compare (with the if command) this variable (DURATION) with a preset time set by me within the programming itself? Or do something similar. Example:

if %DURATION% LSS "00:00:01:00" echo Texto.

1 answer

3


The code you are using is this:

@echo off
rem Inicio do procedimento.
set STARTTIME=%TIME%

rem Coloque o procedimento que deseja medir o tempo.
pause

rem Fim do procedimento.
set ENDTIME=%TIME%

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)
set /A DURATION=%ENDTIME%-%STARTTIME%

if %ENDTIME% LSS %STARTTIME% set set /A DURATION=%STARTTIME%-%ENDTIME%

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)

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%

echo Tempo do procedimento: %DURATIONH%:%DURATIONM%:%DURATIONS%

pause > nul

The variable %DURATION% stores time in hundredths of a second, so to check if it took less than one minute you should make the following check:

if %DURATION% LSS 6000 (
  echo Durou menos que um minuto.
) else (
  echo Durou um minuto ou mais.
)
pause > nul

1 second = 100 hundredths of a second. 1 minute = 6000 hundredths of a second.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.