1. Computer problem? Tech Support Guy is completely free -- paid for by advertisers and donations. Click here to join today! If you're new to Tech Support Guy, we highly recommend that you visit our Guide for New Members.

Date Subtraction using System Date in batch file

Discussion in 'DOS/Other' started by Ananth_Maniam, Jan 28, 2013.

Thread Status:
Not open for further replies.
Advertisement
  1. Ananth_Maniam

    Ananth_Maniam Thread Starter

    Joined:
    Jan 28, 2013
    Messages:
    1
    Hi Guys ,

    I want to dynamically change the dates in my script name using system date

    Say i am submitting a script on 28th Jan i want my script name to look like

    abc_2013-01-21_To_2013-01-27

    Another example Say i'm submitting a script on 4th Feb i want my script name to look like

    abc_2013-01-28_To_2013-02-03

    i.e., i want my script to run for the previous week ( Monday through Sunday)

    I'm stuck on this for a long time :( May be this question has been answered already ( My searches in the forum didn't yield results)

    Many thanks
    Ananth
     
  2. shirulkar

    shirulkar

    Joined:
    Jan 24, 2013
    Messages:
    5
    Following code will work as you required.My system date format is "mm-dd-yyyy". If it is different for your system then you need to change code accordingly. I have just pinted the file name on consol. you can use it as required.


    REM @ECHO OFF
    REM set /p DATE=Enter date in MM/DD/YYYY format:
    REM set /p Days=Enter days:
    set Day=-7
    CALL :DATETOJULIAN %DATE%
    SET /A NEWDATE=JDN+%Day%
    set Day1=-1
    CALL :JULIANTODATE %NEWDATE%
    SET /A NEWDATE1=JDN+%Day1%
    CALL :JULIANTODATE1 %NEWDATE1%

    Rem output
    ECHO abc_%YY%-%MM%-%DD%_To_%YY1%-%MM1%-%DD1%

    exit /b

    :DATETOJULIAN
    FOR /F "TOKENS=1-3 DELIMS=-" %%A IN ("%1") DO SET MM=%%A& SET DD=%%B& SET YY=%%C
    SET /A DD=10%DD% %% 100, MM=10%MM% %% 100
    IF %MM% LSS 3 SET /A MM+=12, YY-=1
    SET /A A=YY/100, B=A/4, C=2-A+B, E=36525*(YY+4716)/100, F=306*(MM+1)/10, JDN=C+DD+E+F-1524
    ECHO abc_%YY%-%MM%-%DD%

    exit /b

    :JULIANTODATE
    SET /A W=(%1*100-186721625)/3652425, X=W/4, A=%1+1+W-X, B=A+1524, C=(B*100-12210)/36525, D=36525*C/100
    SET /A E=(B-D)*10000/306001, F=306001*E/10000, DD=B-D-F, MM=E-1, YY=C-4716
    IF %MM% GTR 12 SET /A MM-=12, YY+=1
    IF %DD% LSS 10 SET DD=0%DD%
    IF %MM% LSS 10 SET MM=0%MM%
    ECHO abc_%YY%-%MM%-%DD%

    exit /b

    :JULIANTODATE1
    SET /A W=(%1*100-186721625)/3652425, X=W/4, A=%1+1+W-X, B=A+1524, C=(B*100-12210)/36525, D=36525*C/100
    SET /A E=(B-D)*10000/306001, F=306001*E/10000, DD1=B-D-F, MM1=E-1, YY1=C-4716
    IF %MM1% GTR 12 SET /A MM1-=12, YY1+=1
    IF %DD1% LSS 10 SET DD1=0%DD1%
    IF %MM1% LSS 10 SET MM1=0%MM1%

    exit /b
     
  3. shirulkar

    shirulkar

    Joined:
    Jan 24, 2013
    Messages:
    5
    continue with above post.
    If your date format is DD-MM-yyyy then just change one line

    FOR /F "TOKENS=1-3 DELIMS=-" %%A IN ("%1") DO SET MM=%%A& SET DD=%%B& SET YY=%%C

    To

    FOR /F "TOKENS=1-3 DELIMS=-" %%A IN ("%1") DO SET DD=%%A& SET MM=%%B& SET YY=%%C
     
  4. foxidrive

    foxidrive Banned

    Joined:
    Oct 20, 2012
    Messages:
    793
    Here is a VBS solution that is region independent:

    This batch file calls the batch file beneath it:

    Code:
    @echo off
    call Date_foward_and_backward.bat today -1
    set two=%day%
    call Date_foward_and_backward.bat today -8
    set one=%day%
    
    echo "abc_%one%_To_%two%
    
    pause





    :: Date_foward_and_backward.bat

    Code:
    @echo off
    :: from code by Phil Robyn
    setlocal
    if [%1]==[] (
      echo to get todays date use
      echo call "%~n0" today 0
      echo.
      echo to get yesterdays date use
      echo call "%~n0" today -1
      echo.
      echo to get the date 25 days ago:
      echo call "%~n0" today -25
      echo.
      echo to get the date 1250 days in the future
      echo call "%~n0" today +1250
      goto :EOF)
    
    set date1=%1
    set qty=%2
    if /i "%date1%" EQU "TODAY" (
     set date1=now
    ) else (
     set date1="%date1%"
    )
    echo >"%temp%\%~n0.vbs" s=DateAdd("d",%qty%,%date1%)
    echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^&_
    echo>>"%temp%\%~n0.vbs"         right(100+month(s),2)^&_
    echo>>"%temp%\%~n0.vbs"         right(100+day(s),2)
    for /f %%a in (
      'cscript //nologo "%temp%\%~n0.vbs"') do set result=%%a
    del "%temp%\%~n0.vbs"
    endlocal& set day=%result:~0,4%-%result:~4,2%-%result:~6,2%
    echo %%day%% is set to "%day%" (without the quotes)
     
As Seen On
As Seen On...

Welcome to Tech Support Guy!

Are you looking for the solution to your computer problem? Join our site today to ask your question. This site is completely free -- paid for by advertisers and donations.

If you're not already familiar with forums, watch our Welcome Guide to get started.

Join over 733,556 other people just like you!

Thread Status:
Not open for further replies.

Short URL to this thread: https://techguy.org/1087191

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice