Calculate two times in a variable in batch



  • 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 make the calculations between initial and final time.

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

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

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


  • 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 second, so to check if it took less than a minute to do 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 second. 1 minute = 6000 cm.




Suggested Topics

  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2
  • 2