Welcome, guest | Sign In | My Account | Store | Cart

The trick to add days to a date is to convert the date to a serial, do a normal addition then convert the result back to a date. The serial I use here is the Julian day number used by the astronomers because its conversion formulas arewell documented. If you check the julian day number on internet the result can vary by a day, because julian days start at noon and i'm not making this distinction.

Batch, 31 lines
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
@ECHO OFF
setlocal enabledelayedexpansion
set /a y=2016, m=6, d=16 
call:addays -100 y m d
echo %y% %m% %d%
pause
exit /b

:addays  %n% &Y &m &d
setlocal
set /a y=!%2!,m=!%3!,d=!%4!
call:date2jdn  %y% %m% %d% jdn
set /a  jdn+=%1
call:jdn2date %jdn% y m d
endlocal & set /a %2=%y%,%3=%m%,%4=%d%
exit /b

:date2jdn %YYYY% %MM% %DD% &JDN -- gregorian date to jyulian day number
setlocal
IF %2 LSS 3 (SET /A MM=%2+12, YY=%1-1) else (set /a MM=%2, YY=%1)
SET /A A=YY/100, B=A/4, C=2-A+B, E=36525*(YY+4716)/100, F=306*(MM+1)/10, JDN=C+%3+E+F-1524
endlocal & set %~4=%JDN%
exit /b

:jdn2date %JDN% &YYYY &MM &DD -- julian day number to gregorian date
SETLOCAL  
set /a L= %~1+68569,     N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447,  K= L-2447*J/80,      L= J/11
set /a J= J+2-12*L,      I= 100*(N-49)+I+L
ENDLOCAL & SET /a %~2=%I%, %~3=%J%,%~4=%K%
EXIT /b
Created by Antoni Gual on Thu, 16 Jun 2016 (MIT)
Batch recipes (42)
Antoni Gual's recipes (16)

Required Modules

  • (none specified)

Other Information and Tasks