Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


Subtract time in dos batch file

Posted on 2012-09-18
Medium Priority
Last Modified: 2012-09-20
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:

set HH=%TIME:~0,2%
set MM=%TIME:~3,2%
set SS=%TIME:~6,2%

set /a SECS_IN_MINS=%MM% * 60

set /a SECS_IN_HOURS=%HH% * 3600


set /a NEW_SECS=%TOTAL_SECS% - 30

Open in new window

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???
Question by:Wanting2LearnMan
  • 3
  • 2
  • 2
  • +1

Expert Comment

ID: 38410263
I have used this before to do something similar...

LVL 59

Accepted Solution

Bill Prew earned 2000 total points
ID: 38410331
I love DOFF free utility for things like this, it sames a lot of work.  Check it out at this link, help screen is below.  In a BAT script you can use a simple FOR command to get the output of the DOFF calculation off subtracting seconds, and it will handle the day overlaps fine, etc.  Let me know if you want help implementing for your specific example...


doff - Prints a formatted date string offset by a specified time interval
syntax is: doff [format_str [offset_str [exclude_format_str]]]
       or: doff -h   (displays this help screen)

format_str, (case sensitive), may include any of the following:
  yyyy - replaced by four digit year in output string
  yy - replaced by two digit year in output string
  mm - replaced by month (1 - 12) in output string
  dd - replaced by day (1 - 31) in output string
  hh - replaced by hour (0 - 23) in output string
  mi - replaced by minute (0 - 59) in output string
  ss - replaced by second (0 - 59) in output string (leap seconds = 59)
Other characters in format_str are returned as-is

offset_str, (case sensitive), is specified as [+/-]n[smhd]
  "n" is the offset value
  offset qualifiers are: s = seconds, m = minutes, h = hours, d = days

exclude_format_str has the same format as format str.
Patterns in exclude_format_str that would normally be translated are ignored.
For example, including "ss" in exclude_format_str means "ss" will appear
untranslated in the output string.

Default offset value is zero, default offset qualifier is days
Bad offset values default to zero, bad offset qualifiers default to days
Note: -10xd will evaluate to "10 days ago", (because atoi("-10x") = -10)
  but -x10d will evaluate to "now", (because atoi("-x10") = 0)

When both format_str and offset_str are omitted the output format is:

Open in new window


Author Comment

ID: 38410361
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:

How do I use your solution to do this??

Restore individual SQL databases with ease

Veeam Explorer for Microsoft SQL Server delivers an easy-to-use, wizard-driven interface for restoring your databases from a backup. No expert SQL background required. Web interface provides a complete view of all available SQL databases to simplify the recovery of lost database

LVL 59

Expert Comment

by:Bill Prew
ID: 38410384
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%

Open in new window

LVL 43

Expert Comment

by:Steve Knight
ID: 38410411
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 1. Just show on screen
echo wscript.echo DateAdd("s",-30,Now()) >"%temp%\getdate.vbs"
cscript //nologo "%temp%\getdate.vbs"


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%
LVL 43

Expert Comment

by:Steve Knight
ID: 38410429
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:


Author Closing Comment

ID: 38417841
Excellent utility :)
LVL 59

Expert Comment

by:Bill Prew
ID: 38418460
Great, glad you liked it, as I do, thanks for the feedback.


Featured Post

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Batch, VBS, and scripts in general are incredibly useful for repetitive tasks.  Some tasks can take a while to complete and it can be annoying to check back only to discover that your script finished 5 minutes ago.  Some scripts may complete nearly …
This is a fine trick which I've found useful many times, when you just don't want to accidentally run a batch script or the commands needs administrator rights.
In this fifth video of the Xpdf series, we discuss and demonstrate the PDFdetach utility, which is able to list and, more importantly, extract attachments that are embedded in PDF files. It does this via a command line interface, making it suitable …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question