Here is an interesting comparison between scripting languages. I was playing with getting the time 1 minute into the future in order to set up a scheduled command. So I needed the time to be in the following format: HH:MM
So since I was playing with the at.exe I figured a batch would be easy enough, surely. Well as it turns out, not so, as you can see from the code below a little more work than I expected was required. Now admittedly there may be other ways of doing this much better, man I sure hope so, however this is what I came up with.
Batch File
[cmd]
@ECHO OFF
ECHO.
ECHO Add x minute(s) to the current time
ECHO.
REM Only use minutes less than 60. or else the calculations fail.
SET /A addmin=1
SET nowtime=%time:~0,5%
SET /A nowmin=%nowtime:~3,2%
SET /A nowhour=%nowtime:~0,2%
REM now add x minute(s) to the time
SET /A nowmin=%nowmin%+%addmin%
REM Future change: if gtr 60 divide by 60 to determine the number of hours to
REM add to the now hour variable. And the left over will be the minutes to add
REM then if the extra mins takes it over another hour, then add this as well.
IF %nowmin% GTR 59 (
SET /A nowmin=%nowmin%-60
SET /A nowhour=%nowhour%+1
)
REM If nowhour has gone past 12
IF %nowhour% GTR 12 SET /A nowhour=%nowhour%-12
REM Test the value is less than 10 and add a leading zero
if %nowmin% LSS 10 (
REM echo “Less than 10”
SET nowmin=0%nowmin%
) else (
REM echo “Higher than 10”
)
SET thistime=%nowhour%:%nowmin%
ECHO %nowtime%
ECHO %thistime%
[/cmd]
Then I got to thinking, how would I do this in some other languages?
VBScript
FormatDateTime(DateAdd("n", 1, Now()), VBShortTime)
Powershell
Get-Date((Get-Date).AddMinutes(1)) -Format 'HH:mm'
Significantly shorter coding in either of vbscript or powershell. Was to be expected really, however interesting none the less.