General purpose date math routine.

In tip 531, we created JSIToday to parse today's date and JSIExpDT to parse a user's password expiration date.

Here is JSIDateM, a general purpose date math routine with the following usage syntax and fiunctionality:

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 JSIDateM detects an error, arithmetic environment variable NDD is set to 999999.

In an arithmetic environment variable, leading zeros are stripped. January would appear in NMM as 1 but would be 01 in AMM. NYY is in YYYY format,
even if YY1 and/or YY2 are in YY format. AYMD is in YYYYMMDD format.

NOTE: JSIDateM.bat has been modified to call Date2JD.bat and JD2Date.bat.

Create JSIDateM.bat in your path:

```@echo off
setlocal
If /i "%4""+" goto OK1
If /i "%4""-" goto OK1
if "%4""" goto OK1
:syntax
@echo JSIDateM YY MM DD +/- Days/YY MM DD
set /a NDD=999999
goto end
:OK1
set /a YY1=%1
set /a MM1=100%2%%100
set /a DD1=100%3%%100
If not "%6""" goto OKD
if "%4""" goto OK5
set DAYA=%5
set /a DAYN=10000%DAYA%%%10000
if "%DAYA%""%DAYN%" goto DAYS
goto syntax
:OK5
set /a NDD=%DD1%
set /a NMM=%MM1%
set /a NYY=%YY1%
set AMM=%NMM%
set ADD=%NDD%
if %NMM% LSS 10 set AMM=0%NMM%
if %NDD% LSS 10 set ADD=0%NDD%
set AYMD=%YY1%%AMM%%ADD%
goto end
:OKD
If /i "%4""+" goto syntax
set /a YY2=%5
set /a MM2=100%6%%100
set /a DD2=100%7%%100
call date2JD %YY1% %MM1% %DD1% JD1
call date2JD %YY2% %MM2% %DD2% JD2
set /a NDD=%JD1% - %JD2%
:: The following is to prevent errors due to undefined variables
set ADD=%DD1%
if %DD1% LSS 10 set ADD=0%ADD%
set AMM=%MM1%
set /a NMM=%MM1%
if %NMM% LSS 10 set AMM=0%NMM%
set /a NYY=%YY1%
set AYMD=%YY1%%AMM%%ADD%
goto end
:DAYS
call date2JD %YY1% %MM1% %DD1% JD1
If /i "%4""+" goto DAYSP
set /a JD1=%JD1% - %DAYN%
:DAYSE
call JD2Date %JD1% YY1 AMM ADD
set /a NDD=100%ADD%%%100
set /a NMM=100%AMM%%%100
set /a NYY=%YY1%
if %NMM% LSS 10 set AMM=0%NMM%
if %NDD% LSS 10 set ADD=0%NDD%
set AYMD=%YY1%%AMM%%ADD%
goto end
:DAYSP
set /a JD1=%JD1% + %DAYN%
goto DAYSE
:end
Endlocal&set /a NYY=%NYY%&set /a NMM=%NMM%&set /a NDD=%NDD%&set AYMD=%AYMD%&set AMM=%AMM%&set ADD=%ADD%```
See tip 559 for an sample use.

To calculate the number of days till Xmas, XMAS.bat contains:

```@echo off
call univdate
call jsidatem %yy% 12 25 - %yy% %mm% %dd%
@echo %NDD%```

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