0

I am trying to compare two timestamps (timeA against timeB) in the format of HH:MM without seconds, so that I can see if timeB has a time which is later than 30 minutes or more than the time of timeA. I can assume that timeB will always be equal or (most commonly) later than timeA.

REM 12:42
set "hourA=12"
set "minA=42"

REM 12:49
set "hourB=12"
set "minB=49"

if %hourA% equ %hourB% (
  if %minA% equ %minB% (
    echo A is equal to B!
  ) else (
    echo A is NOT equal to B!
  )
)

I find really hard to implement the logic of checking if timeB is greater or equal than 30 minutes compared to timeB for cases like timeA being 12:42 and timeB being 13:15 where you need to check for the hours first, then the minutes and then do another check for the hours.

Any suggestions or example code would be great (I am relatively new to batch scripts). The batch script is run on Windows.

Mofi
  • 46,139
  • 17
  • 80
  • 143
  • 1
    See [Find out if file is older than 4 hours in Batch file](https://stackoverflow.com/questions/6928552/) . The code examples in the answers can be modified for 30 minutes difference instead of 4 hours. For time difference check within a day, multiply twice the hour with 60 and add the minute using `set /a` and compare the two integer values using `if`. – Mofi Jul 06 '17 at 08:45

1 Answers1

1
@ECHO OFF
SETLOCAL
SET "timestra=12:42"
SET "timestrb=03:01"

SET /a timediff=(1%timestrb:~0,2% - 1%timestra:~0,2%) * 60 + (1%timestrb:~-2% - 1%timestra:~-2%)
IF %timediff% lss 0 SET /a timediff+=60*24
ECHO TIME difference=%timediff%

GOTO :EOF

Given two valid timestrings in 24-hr format, set the time difference to 60*((100+the last HH) - (100 + the first HH)) + ((100+the last MM) - (100 + the first MM))

The 100+ is implemented by stringing 1 in front of the first/last 2 characters of the timestring. This overcomes batch's "leading 0 is octal" dictum since the leading digit in both cases is 1.

If the result of the overall calculation is negative, then we have crossed midnight, so add an entire day's worth of minutes. This will of course only work if the distance between the two times is less than 24 hours, which is probably adequate given the presented scenario.

then it's a simple matter of

if %timediff% gtr 30 (echo more than 30) else (echo less than 30)
Magoo
  • 77,302
  • 8
  • 62
  • 84
  • Thanks for the detailed answer. When I run it by itself it works perfectly. However, when I copy paste the commands (lines 3-8) into an existing script I am getting a `0 was unexpected at this time`. Do you have any ideas why that would be? –  Jul 06 '17 at 09:48
  • Need to see precisely what you have in the batch and the contents of the two timestrings. You are, of course, setting the timestrings appropriately, aren't you? Also, it may depend on the precise location within the batch - if it is within a `code block` (a parenthesised series of lines) then you'd need to have `delayed expansion` in effect and use `!` in place of `%` within that pasted code (see SO using the `search` facility on the top bar for extensive documentation on `delayed expansion` if required) – Magoo Jul 06 '17 at 10:24
  • Apologies, I did fix it was a parenthesis issue. Thanks –  Jul 06 '17 at 10:33