Q. Another way to calculate date plus or minus n days?

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:

<b>NewYYYY-MM-DD</b> is a <a href="/article/tips/tip.aspx?id=5535">call directed numeric environment variable</a> that will contain the resulting YYYY-MM-DD.

<b>DATEorTODAY</b>   is a valid short date, or the literal <b>TODAY</b>.

<b>+or-NN</b>        is the number to add or subtract from <b>DATEorTODAY</b>, like <b>0</b>, or <b>+1</b>, or <b>-1</b>, or <b>+365</b>.
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%


Please or Register to post comments.

IT/Dev Connections

Las Vegas
September 30th - October 4th

Paul ThurottYou'll have the opportunity to experience:
• The Microsoft
Technology Roadmap
• Office 365 Implementation
• Hyper-V Optimizing
• Windows 8 Deployment
and much more!

Come See Paul Thurrott & Rod Trent in Person!

Early Registration Now Open

Upcoming Training

Mastering SharePoint 2013: Succeeding, Not Just Surviving

Building on the success of the “Mastering SharePoint 2010” seminars, the presenters have updated the content to cover the latest and greatest SharePoint product: SharePoint 2013. While SharePoint 2013 is relatively new on the marketplace, the presenters have been working with SharePoint 2013 for well over a year, and have implemented it with a number of clients in production environments.

Register Now

Current Issue

May 2013 - The NameTranslate object is useful when you need to translate Active Directory object names between different formats, but it's awkward to use from PowerShell. Here's a PowerShell script that eliminates the awkwardness.

CURRENT ISSUE / ARCHIVE / SUBSCRIBE

Windows Forums

Get answers to questions, share tips, and engage with the Windows Community in our Forums.