Search

Adding XX minutes to the current time in a Windows batch file

It has been a very long time since I have written any Windows/Dos batch files... probably since my DOS class in College. That being the case, there may very well be an easier/better solution to this but I could not find it with a quick search on Google. Since I couldn't find it, I wrote this simple script.

What I needed was a very simple requirement... add 10 minutes to the current time so it can be used to create a scheduled task that would execute 10 minutes from now.

I searched Google but all I found were ways to add days and overly complicated dateAdd functions (that I didn't feel like deciphering just for minutes)

So, basically, all the code does is get the current time and extract the HOUR and MINUTE values. It then adds 10 to the minute value. If minutes exceed 59, it rolls the hour up 1 and subtracts 60 from the minutes. It then checks to see if the hour is greater than 24 (if it added 1 to 24, it would be 25 of course). If it is, it simply changes it to 0.

Once the new hour and minute values have been determined, it simply puts them together with a : and appends the seconds that were in the original time.

So, for example, if the current time was 24:56:42.10, the result of this routine would be: 00:06:42.10.

If the current time was 23:49:42.10, the result of this routine would be: 23:59:42.10.

This, combined with a simple AT command gave me exactly what I needed.


::============================================
::GET THE CURRENT HOUR AND MINUTE + 10 MINUTES
::============================================
SET CURRENTTIME=%TIME%
for /F "tokens=1 delims=:" %%h in ('echo %CURRENTTIME%') do (set /a HR=%%h)
for /F "tokens=2 delims=:" %%m in ('echo %CURRENTTIME%') do (set /a MIN=%%m + 10)


::=======================================================
::IF THE MINUTE IS >= 60, ROLL IT OVER BY SUBSTRACTING 60
::FROM MINUTES AND ADDING 1 TO HOURS
::=======================================================
IF %MIN% GEQ 60 (
SET /a MIN=%MIN%-60
SET /a HR=%HR%+1
)

::===========================================================================
::IF THE HOUR IS > 24, THEN IT IS BECAUSE WE ADDED 1 TO 24 ABOVE. SET IT TO 0
::===========================================================================
IF %HR% GTR 24 SET HR=00


::============================================
::PAD SINGLE DIGIT MINUTES WITH A LEADING ZERO
::============================================
IF %MIN% LEQ 9 (
SET MIN=0%MIN%
)

::==========================================
::PAD SINGLE DIGIT HOURS WITH A LEADING ZERO
::==========================================
IF %HR% LEQ 9 (
SET HR=0%HR%
)

::========================================================================
::USE THE NEW HOUR AND MINUTE (AND EXISTING SECONDS) TO CREATE THE NEW TIME
::========================================================================
SET NEWTIME=%HR%:%MIN%:%CURRENTTIME:~6,10%



Note that this could be very easily altered to add hours instead of minutes. You can also replace the '10' with %1 to be able to pass in the number of minutes.

For instance:


C:\> add10minutes.bat 20

Most Recent Photos