Add minutes to time in batch (using arithmetic)

2019-02-18 22:56发布

问题:

I'm running into a dilemma. I want to send a preemptive email to users telling them that an upcoming task is about to happen. In order to do that I'm defining some variables, sending an email via blat, sleeping the batch for 5 minutes, then executing the rest of the script.

When executing %time% at 4:00PM, I get 16:00:00.00. If I add 5 minutes to it, only for display purposes in the email with the following code:

@echo on
SET /a timeminute = 00 + 5 << --- test code
::SET /a timeminute = %time:~3,2% + 5 << --- actual code in GoLive
IF %timeminute% LEQ 9 (
    GOTO :resetTime
) ELSE (
    GOTO :end
)
:resetTime
SET timeminute = "0%timeminute%"

:end
echo %timeminute%
pause

I get 5, not 05 like expected. Using arithmetic on time drops leading zeros, so I try to add it back in later but the later SET is within the IF statement and cannot be seen? How can I see that? Is there a such thing as an environment variable in batch?

Keep in mind this issue only happens within the first 9 minute of the hour, after that time, there are no more leading zeros.

Bonus: What happens when the minutes in a hour is 55-59? In my example, it will be 60-64, so I need a way of rounding up an hour and take care of the remaining minutes. Right now, I see that as a bug, but I do not foresee this script running at those odd times. But if it is an easy fix please let me know as I have not even tried to tackle that problem.

Thank you kindly

回答1:

A more compact form to do the same thing is this:

@echo on
for /F "tokens=1-3 delims=:." %%a in ("%time%") do (
   set timeHour=%%a
   set timeMinute=%%b
   set timeSeconds=%%c
)
rem Convert HH:MM to minutes + 5
set /A newTime=timeHour*60 + timeMinute + 5
rem Convert new time back to HH:MM
set /A timeHour=newTime/60, timeMinute=newTime%%60
rem Adjust new hour and minute
if %timeHour% gtr 23 set timeHour=0
if %timeHour% lss 10 set timeHour=0%timeHour%
if %timeMinute% lss 10 set timeMinute=0%timeMinute%
echo %timeHour%:%timeMinute%:%timeSeconds%
pause


回答2:

Answered my own question with the following:

@echo on
setlocal enabledelayedexpansion

set timehour=%time:~0,2%
set timeminute=%time:~3,2%
set timeseconds=%time:~6,2%
set addTime=5
IF %timeminute:~0,1% lss 1 set timeminute=!timeminute:~1,1!
IF %timeminute:~0,1% lss 1 set timeminute=!timeminute:~1,1!
set /a timeminute=%timeminute% + %addTime%
IF %timeminute% lss 10 set timeminute=0!timeminute!

IF %timeminute% equ 60 (
set timeminute=00
set /a timehour=%timehour% + 1
)

IF %timeminute% equ 61 (
set timeminute=01 
set /a timehour=%timehour% + 1
)

IF %timeminute% equ 62 (
set timeminute=02 
set /a timehour=%timehour% + 1
)

IF %timeminute% equ 63 (
set timeminute=03
set /a timehour=%timehour% + 1
)

IF %timeminute% equ 64 (
set timeminute=04
set /a timehour=%timehour% + 1
)

IF %timehour% equ 25 (
set timehour=00
)

IF %timehour% lss 10 set timehour=0!timehour!

echo %timehour%:%timeminute%:%timeseconds%
pause