Date Subtraction using System Date in batch file

Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

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

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)
 
Status
This thread has been Locked and is not open to further replies. Please start a New Thread if you're having a similar issue. View our Welcome Guide to learn how to use this site.

Users Who Are Viewing This Thread (Users: 0, Guests: 1)

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 807,865 other people just like you!

Latest posts

Members online

Top