Wanting2LearnMan
asked on
Subtract time in dos batch file
I need to get the current date and time and subtract a number of seconds form it in a dos batch file.
So far I have did the following:
I will then convert this to the new time.
But this will fallover when it changes the midnight boundary.
Is there a reliable way of doing this???
So far I have did the following:
set HH=%TIME:~0,2%
set MM=%TIME:~3,2%
set SS=%TIME:~6,2%
ECHO %HH%:%MM%:%SS%
ECHO %MM%
set /a SECS_IN_MINS=%MM% * 60
ECHO %SECS_IN_MINS%
ECHO %SECS_IN_HOURS%
set /a SECS_IN_HOURS=%HH% * 3600
ECHO %SECS_IN_HOURS%
set /a TOTAL_SECS=%SECS_IN_HOURS% + %SECS_IN_MINS% + %SS%
ECHO %TOTAL_SECS%
set /a NEW_SECS=%TOTAL_SECS% - 30
ECHO %NEW_SECS%
I will then convert this to the new time.
But this will fallover when it changes the midnight boundary.
Is there a reliable way of doing this???
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks bp this is great.
What I want to do is run a batch file that calls a program passing it two date&time variables like so:
myProgram currentDateAndTIme (currentDateAndTIme - e.g. 30 seconds)
The currentDateAbdTime is in the below format:
2012-08-28:13:00:00
How do I use your solution to do this??
Thanks
What I want to do is run a batch file that calls a program passing it two date&time variables like so:
myProgram currentDateAndTIme (currentDateAndTIme - e.g. 30 seconds)
The currentDateAbdTime is in the below format:
2012-08-28:13:00:00
How do I use your solution to do this??
Thanks
See if this gives you the idea for the code in your BAT file. We can format as you need, and get the current time, and then the time 30 seconds ago. Then just use the environment variables where you need to pass those times.
for /F "tokens=*" %%A in ('doff "yyyy-mm-dd:hh:mi:ss"') do set Current=%%A
for /F "tokens=*" %%A in ('doff "yyyy-mm-dd:hh:mi:ss" -30s') do set Prior=%%A
echo Current=%Current%
echo Prior=%Prior%
~bp
Is VBScript an option, or one-line of VBScript called from a batch file as it has built in functions for such things:
wscript.echo DateAdd("s",-30,Now())
is 30 secs ago... s can be s,n,h,d,m,y etc. for secs, mins, hours, days, months, years and you can d that from cmd file with:
@echo off
REM Time 30 seconds ago.
ECHO.
ECHo 1. Just show on screen
echo wscript.echo DateAdd("s",-30,Now()) >"%temp%\getdate.vbs"
cscript //nologo "%temp%\getdate.vbs"
ECHO.
ECHO 2. Get into a variable
for /f "tokens=1,2" %%a in ('cscript //nologo "%temp%\getdate.vbs"') do set d=%%a&set t=%%b
echo Date and time 30 secs ago was %d% and %t%
wscript.echo DateAdd("s",-30,Now())
is 30 secs ago... s can be s,n,h,d,m,y etc. for secs, mins, hours, days, months, years and you can d that from cmd file with:
@echo off
REM Time 30 seconds ago.
ECHO.
ECHo 1. Just show on screen
echo wscript.echo DateAdd("s",-30,Now()) >"%temp%\getdate.vbs"
cscript //nologo "%temp%\getdate.vbs"
ECHO.
ECHO 2. Get into a variable
for /f "tokens=1,2" %%a in ('cscript //nologo "%temp%\getdate.vbs"') do set d=%%a&set t=%%b
echo Date and time 30 secs ago was %d% and %t%
Was a bit slow in hitting submit there.... gives you another way anyway. This or DOFF all better than using %date% and %time% due to format changes, system differences etc. See here for more:
https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html
Steve
https://www.experts-exchange.com/OS/Microsoft_Operating_Systems/MS_DOS/A_1153-Using-dates-in-batch-files-scripts.html
Steve
ASKER
Excellent utility :)
Great, glad you liked it, as I do, thanks for the feedback.
~bp
~bp
http://ss64.com/nt/syntax-tdiff.html