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


In the General purpose date math routine, I support the following date arithmetic:

Call JSIDateM YY1 MM1 DD1 - YY2 MM2 DD2 - Sets arithmetic environment variable NDD to the signed difference.

Call JSIDateM YY1 MM1 DD1 +/- Days -Sets arithmetic environment variables NYY, NMM, and NDD,
and sets environment variables AMM, ADD and AYMD to the result of the addition or substraction.

Call JSIDateM YY1 MM1 DD1 - Sets arithmetic environment variables NYY, NMM, and NDD,
and sets environment variables AMM, ADD and AYMD

If all you need is to return a date, in your normal date format, that is plus or minus n days from today, use DatePorM.bat.

The syntax for using DatePorM.bat is:

call DatePorM SignedNumber ResultingDate

Where:

SignedNumber  is the number of day you wish to add or subtract from today, like -1 or +1. The + sign is optional.

ResultingDate   is a call directed environment variable that will contain the resultant date, in your short date format.

Examples:

If today is 07/23/2004, then:
call DatePorM +365 NextYear
@echo %NextYear%

would display 07/23/2005

call DatePorM -1 Yesterday
@echo %Yesterday%

would display 07/22/2004
NOTE: Ammended August 22, 2005.

DatePorM.bat contains:

@echo off
if \{%2\}==\{\} @echo Syntax: call DatePorM SignedNumber ResultingDate&goto :EOF
setlocal
set dpom="%temp%\DatePorM_%RANDOM%.vbs"
@echo WScript.Echo DateAdd("d", %1, Date) >%dpom%
for /f "tokens=*" %%a in ('cscript.exe //Nologo %dpom%') do (
 set dt=%%a
)
del /q %dpom%
set /a cnt=0
:loop
call set char=%%dt:~%cnt%,1%%
set /a cnt=%cnt% + 1
if "%char%" LSS "0" goto sep
if "%char%" GTR "9" goto sep
goto loop
:sep
for /f "Tokens=1-3 Delims=%char%# " %%a in ('@echo %dt%') do (
 set /a p1=10000%%a%%10000
 set /a p2=10000%%b%%10000
 set /a p3=10000%%c%%10000
)
if %p1% LSS 10 set p1=0%p1%
if %p2% LSS 10 set p2=0%p2%
if %p3% LSS 10 set p3=0%p3%
endlocal&set %2=%p1%%char%%p2%%char%%p3%



Hide comments

Comments

  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.
Publish