Windows IT Pro is the authoritative and independent resource for windows nt, windows 2000, windows 2003, windows xp. Features a collection of resources and magazines for windows IT professionals.
  
  
  Advanced Search 


December 27, 2006

Another way to calculate date plus or minus n days?

RSS
Subscribe to Windows IT Pro | See More Tips Articles Here | Reprints | Or get the Monthly Online Pass—only $5.95 a month!

11130 » Another way to calculate date plus or minus n days? (27-Dec-06)

I had scripted previous date calculations, some of which are:

8293 » How can I return the date that is plus or minus n days from today?

9977 » How can I calculate the difference between two short dates?

0721 » General purpose date math routine.

I have now scripted PorMdays.bat to return a date in YYYY-MM-DD format that is the result of adding or subtracting n days from a short date, or from TODAY.

The syntax for using PorMdays.bat is:

[call] PorMdays NewYYYY-MM-DD DATEorTODAY +or-NN

Where:

NewYYYY-MM-DD is a call directed numeric environment variable that will contain the resulting YYYY-MM-DD.

DATEorTODAY   is a valid short date, or the literal TODAY.

+or-NN        is the number to add or subtract from DATEorTODAY, like 0, or +1, or -1, or +365.
NOTE: Because NewYYYY-MM-DD is a valid short date, it can be used in subsequent calculations, as in the following silly example:
call PorMdays Thisday today 0
call PorMdays Yesday %Thisday% -1
call PorMdays Tomday %Yesday% +2
@echo Today is %Thisday%, Yesterday was %Yesday%, and Tomorrow is %Tomday%
PorMdays.bat contains:
@echo off
if {%4} NEQ {} @echo Syntax: [call] PorMdays NewYYYY-MM-DD DATEorTODAY +or-NN (call PorMdays YMD today -1)&goto :EOF
if {%3} EQU {} @echo Syntax: [call] PorMdays NewYYYY-MM-DD DATEorTODAY +or-NN (call PorMdays YMD today -1)&goto :EOF
setlocal
if /i "%2" EQU "TODAY" (
 set DT=now
) else (
 set DT="%2"
)
set RND=%RANDOM%
@echo.s=DateAdd("d",%3,%DT%) >"%temp%\%~n0_%RND%.vbs"
@echo.WScript.Echo year(s)^&right(100+month(s),2)^&right(100+day(s),2) >>"%temp%\%~n0_%RND%.vbs"
for /f %%a in ('cscript //nologo "%temp%\%~n0_%RND%.vbs"') do set YYYYMMDD=%%a
del /q "%temp%\%~n0_%RND%.vbs"
set YMD=%YYYYMMDD:~0,4%-%YYYYMMDD:~4,2%-%YYYYMMDD:~6,2%
endlocal&set %1=%YMD%


End of Article



Reader Comments

You must log on before posting a comment.

If you don't have a username & password, please register now.




Top Viewed ArticlesView all articles
No Jobs, No Excitement at Apple's Last Macworld Keynote

Apple CEO Steve Jobs made the right move in skipping out on his company's last appearance at Macworld: In a Tuesday keynote address at the conference, Apple had no interesting new products to sell, opting instead to spend mind-numbing amounts of time on ...

Where is Microsoft NetMeeting in Windows XP?

...

Command Prompt Tricks

One reader shares his tip for setting up the command prompt to reflect a remote path. ...


Related Events Check out our list of Free Email Newsletters!

Related Resources Become a VIP member of the Windows IT Pro community!
Get it all with the VIP CD and VIP access. A $500+ value for only $279!

Subscribe to Windows IT Pro!
Solve your toughest technical problems with our experts and access 10,000 + articles online. 30% off

Monthly Online Pass - Only $5.95!
Get instant access to 10,000+ articles from Windows IT Pro Magazine!

TechNet Virtual Labs
Evaluate and test Microsoft's newest products.


Windows IT Pro Home Register FAQ for Windows WinInfo News
Europe Edition About Us Contact Us/Customer Service Media Kit Affiliates / Licensing  
SQL Server Magazine Office & SharePoint Pro Windows Dev Pro IT Job Hound ITTV
IT Library Technology Resource Directory Connected Home Windows Excavator Windows SuperSite 
 
 Windows IT Pro is a Division of Penton Media Inc.
 Copyright © 2009 Penton Media, Inc., All rights reserved. Terms and Use | Privacy Statement | Reprints and Licensing