Thursday, April 9, 2009

DOS Command Line Date Math

There are no built in commands to do date math in DOS.  But, it can be done with a series of built in commands.  I used the snippets below to calculate the difference, in days, between the current date and another date.  To do that, I had to first convert the current date to a Julian date.  Once the dates are converted to Julian date, the math is easy – simple integer subtraction.

 

(the Gregorian Calendar is the one you probably use, Julian calendar is based on the number of days since 1/1/4713BC) 

 

:: :: Split Current Date

FOR /F "TOKENS=2-4 DELIMS=/ " %%A IN ('ECHO.%DATE%') DO (

            SET YY=%%C

            SET MM=%%A

            SET DD=%%B

)

 

:: :: Convert Current Date from Gregorian to Julian

IF 1%MM% LSS 110 SET MM=%MM:~1%

IF 1%DD% LSS 110 SET DD=%DD:~1%

SET /A Month1 = ( %MM% - 14 ) / 12

SET /A Year1  = %YY% + 4800

SET /A JDate1  = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %MM% - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %DD% - 32075

 

 

The difference in “JDate1” and the other Julian date is the absolute number of days difference in the dates.

 

 

No comments:

Post a Comment