Subtract time in dos batch file

Posted on 2012-09-18
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
    LVL 9

    Expert Comment

    I have used this before to do something similar...
    LVL 51

    Accepted Solution

    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

    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??

    LVL 51

    Expert Comment

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

    Excellent utility :)
    LVL 51

    Expert Comment

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


    Featured Post

    Threat Intelligence Starter Resources

    Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

    Join & Write a Comment

    In this tutorial I will show you how to make a simple HTML bar chart with the usage of WhizBase, If you want more information about WhizBase please read my previous articles at (http://www.experts-ex…
    Being a system administrator some time we require to do things remotely, one of them is installing software. Here I am going to tell you how to install software through wmic (Windows management instrument console). I am not at all saying that this i…
    Learn the basics of strings in Python: declaration, operations, indices, and slicing. Strings are declared with quotations; for example: s = "string": Strings are immutable.: Strings may be concatenated or multiplied using the addition and multiplic…
    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 …

    729 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

    Need Help in Real-Time?

    Connect with top rated Experts

    19 Experts available now in Live!

    Get 1:1 Help Now