Calculate two times in a variable in the batch

1

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

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

The response variable (DURATION) returns as hour: minute: second: millisecond, how can I compare (with the if statement) this variable (DURATION) with a time predefined by me within the programming itself? Or do something like that. Example:

if %DURATION% LSS "00:00:01:00" echo Texto.
    
asked by anonymous 05.12.2017 / 19:50

1 answer

2

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 the time in hundredths of a second, so to check if it took less than one minute you should do the following verification:

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.

    
05.12.2017 / 21:24