@aGerman,
I tried your program and have a comment on one point. In :YearsAdd and :MonthsAdd routines, when the resulting date have a day that exceed month's days, your code eliminate days in excess and insert the last day of the resulting month (
preserve month). However, there is a different way to obtain the result if we pay attention to the
number of days instead. For example, if the calculation method indicate that the resulting date is 1 day after February/28, but February have just 28 days that year, then the right answer should be March/1. Isn't it? In a similar way than DaysAdd routine...
The Batch file below use this approach to calculate resulting dates. It have just one conversion routine that allows to insert increments for year, month or day, or any combination of the three.
Code: Select all
@echo off
setlocal
echo Subtract 1 year:
echo 2000/02/29
echo -1/ 0/ 0
call :DateDelta 2000/02/29 -1/0/0
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo Subtract 1 month:
echo 2000/02/29
echo 0/-1/ 0
call :DateDelta 2000/02/29 0/-1/0 result=
echo %result%
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo Subtract 1 day:
echo 2000/01/01
echo 0/ 0/-1
call :DateDelta 2000/01/1 0/0/-1
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo What date will be the next year plus 2 months 15 days from today (2013/06/08)?
call :DateDelta 2013/06/08 1/2/15
echo ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
echo And the same date, but 2 years ago?
call :DateDelta 2013/06/08 -2/2/15
goto :EOF
rem Both date and dateDelta must be given in Y/M/D order
rem DateDelta must have 3 numbers, each one with an optional negative sign
:DateDelta dateIn dateDelta [dateOut=]
setlocal
for /F "tokens=1-3 delims=/" %%a in ("%1") do (
for /F "tokens=1-3 delims=/" %%d in ("%2") do (
set /A "yy=%%a+%%d, mm=10%%b%%100+%%e, dd=10%%c%%100, a=(mm-14)/12"
set /A "jdn=(1461*(yy+4800+a))/4+(367*(mm-2-12*a))/12-(3*((yy+4900+a)/100))/4+dd-32075+%%f"
)
)
set /A l=jdn+68569,n=(4*l)/146097,l=l-(146097*n+3)/4,i=(4000*(l+1))/1461001,l=l-(1461*i)/4+31
set /A j=(80*l)/2447,dd=100+l-(2447*j)/80,l=j/11,mm=100+j+2-(12*l),yy=100*(n-49)+i+l
set result=%yy%/%mm:~-2%/%dd:~-2%
endlocal & if "%3" neq "" (set "%3=%result%") else echo %result%
exit /B
Antonio