Powered by GeSHi

Source code for datediff.bat

(view source code of datediff.bat as plain text)

  1. @ECHO OFF
  2. ECHO.
  3.  
  4. :: Check the Windows version
  5. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  6. SETLOCAL
  7.  
  8. :: Check the command line arguments
  9. IF     "%~1"=="" GOTO Syntax
  10. IF NOT "%~3"=="" GOTO Syntax
  11.  
  12. :: Read the Date format from the registry
  13. CALL :ReadDateFormat
  14.  
  15. :: Check if the first date is valid
  16. (ECHO.%1) | FINDSTR /R /B /C:"[0-9]*\%sDate%[0-9]*\%sDate%[0-9]*" >NUL
  17. IF ERRORLEVEL 1 (
  18. 	ECHO Error: %1 is not a valid date
  19. 	ECHO.
  20. 	GOTO Syntax
  21. )
  22. :: Use today if no second date was specified
  23. IF "%~2"=="" (
  24. 	FOR %%A IN (%Date%) DO SET Date2=%%A
  25. ) ELSE (
  26. 	SET Date2=%2
  27. )
  28. :: Check if the second date is valid
  29. (ECHO.%Date2%) | FINDSTR /R /B /C:"[0-9]*\%sDate%[0-9]*\%sDate%[0-9]*" >NUL
  30. IF ERRORLEVEL 1 (
  31. 	ECHO Error: %Date2% is not a valid date
  32. 	ECHO.
  33. 	GOTO Syntax
  34. )
  35.  
  36. :: Parse the first date
  37. CALL :ParseDate %1
  38.  
  39. :: Convert the parsed Gregorian date to Julian
  40. CALL :JDate %GYear% %GMonth% %GDay%
  41.  
  42. :: Save the resulting Julian date
  43. SET JDate1=%JDate%
  44.  
  45. :: Parse the second date
  46. CALL :ParseDate %Date2%
  47.  
  48. :: Convert the parsed Gregorian date to Julian
  49. CALL :JDate %GYear% %GMonth% %GDay%
  50.  
  51. :: Calculate the absolute value of the difference in days
  52. IF %JDate% GTR %JDate1% (
  53. 	SET /A DateDiff = %JDate% - %JDate1%
  54. ) ELSE (
  55. 	SET /A DateDiff = %JDate1% - %JDate%
  56. )
  57.  
  58. :: Format output for singular or plural
  59. SET Days=days
  60. IF %DateDiff% EQU 1 SET Days=day
  61.  
  62. :: Prefix value with a minus sign if negative
  63. IF %JDate% GTR %JDate1% SET DateDiff=-%DateDiff%
  64.  
  65. :: Display the result
  66. ECHO First date  : %1
  67. ECHO Second date : %Date2%
  68. ECHO Difference  : %DateDiff% %Days%
  69.  
  70. :: Return the result in a variable named after this batch file
  71. ENDLOCAL & SET %~n0=%DateDiff%
  72. GOTO:EOF
  73.  
  74.  
  75. ::===================================::
  76. ::                                   ::
  77. ::   -   S u b r o u t i n e s   -   ::
  78. ::                                   ::
  79. ::===================================::
  80.  
  81.  
  82. :JDate
  83. :: Convert date to Julian
  84. :: Arguments : YYYY MM DD
  85. :: Returns   : Julian date
  86. ::
  87. :: First strip leading zeroes; a logical error in this
  88. :: routine was corrected with help from Alexander Shapiro
  89. SET MM=%2
  90. SET DD=%3
  91. IF 1%MM% LSS 110 SET MM=%MM:~1%
  92. IF 1%DD% LSS 110 SET DD=%DD:~1%
  93. ::
  94. :: Algorithm based on Fliegel-Van Flandern
  95. :: algorithm from the Astronomical Almanac,
  96. :: provided by Doctor Fenton on the Math Forum
  97. :: (http://mathforum.org/library/drmath/view/51907.html),
  98. :: and converted to batch code by Ron Bakowski.
  99. SET /A Month1 = ( %MM% - 14 ) / 12
  100. SET /A Year1  = %1 + 4800
  101. SET /A JDate  = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %MM% - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %DD% - 32075
  102. FOR %%A IN (Month1 Year1) DO SET %%A=
  103. GOTO:EOF 
  104.  
  105.  
  106. :ParseDate
  107. :: Parse (Gregorian) date depending on registry's date format settings
  108. :: Argument : Gregorian date in local date format,
  109. :: Requires : sDate (local date separator), iDate (local date format number)
  110. :: Returns  : GYear (4-digit year), GMonth (2-digit month), GDay (2-digit day)
  111. ::
  112. IF %iDate%==0 FOR /F "TOKENS=1-3 DELIMS=%sDate%" %%A IN ('ECHO.%1') DO (
  113. 	SET GYear=%%C
  114. 	SET GMonth=%%A
  115. 	SET GDay=%%B
  116. )
  117. IF %iDate%==1 FOR /F "TOKENS=1-3 DELIMS=%sDate%" %%A IN ('ECHO.%1') DO (
  118. 	SET GYear=%%C
  119. 	SET GMonth=%%B
  120. 	SET GDay=%%A
  121. )
  122. IF %iDate%==2 FOR /F "TOKENS=1-3 DELIMS=%sDate%" %%A IN ('ECHO.%1') DO (
  123. 	SET GYear=%%A
  124. 	SET GMonth=%%B
  125. 	SET GDay=%%C
  126. )
  127. GOTO:EOF
  128.  
  129.  
  130. :ReadDateFormat
  131. :: Read the Date format from the registry.
  132. :: Arguments : none
  133. :: Returns   : sDate (separator), iDate (date format number)
  134. ::
  135. :: First, export registry settings to a temporary file:
  136. START /W REGEDIT /E "%TEMP%.\_TEMP.REG" "HKEY_CURRENT_USER\Control Panel\International"
  137. :: Now, read the exported data:
  138. FOR /F "tokens=1* delims==" %%A IN ('TYPE "%TEMP%.\_TEMP.REG" ^| FIND /I "iDate"') DO SET iDate=%%B
  139. FOR /F "tokens=1* delims==" %%A IN ('TYPE "%TEMP%.\_TEMP.REG" ^| FIND /I "sDate"') DO SET sDate=%%B
  140. :: Remove the temporary file:
  141. DEL "%TEMP%.\_TEMP.REG"
  142. :: Remove quotes from the data read:
  143. :: SET iDate=%iDate:"=%
  144. FOR %%A IN (%iDate%) DO SET iDate=%%~A
  145. :: SET sDate=%sDate:"=%
  146. FOR %%A IN (%sDate%) DO SET sDate=%%~A
  147. GOTO:EOF
  148.  
  149.  
  150. :Syntax
  151. ECHO DateDiff.bat,  Version 1.10 for Windows NT 4 / 2000 / XP / Server 2003 / Vista
  152. ECHO Calculate the difference (in days) between two dates
  153. ECHO.
  154. ECHO Usage:  DATEDIFF  date  [ date ]
  155. ECHO.
  156. ECHO Where:  "date"  is a "normal" Gregorian date in the local computer's format;
  157. ECHO                 if no second date is specified, today is assumed
  158. ECHO.
  159. ECHO Julian date conversion based on Fliegel-Van Flandern algorithms from
  160. ECHO the Astronomical Almanac, provided by Doctor Fenton on the Math Forum
  161. ECHO (http://mathforum.org/library/drmath/view/51907.html), and converted
  162. ECHO to batch code by Ron Bakowski.
  163. ECHO Bug found by and converted with help from Alexander Shapiro.
  164. ECHO.
  165. ECHO Written by Rob van der Woude
  166. ECHO http://www.robvanderwoude.com
  167.  
  168. IF "%OS%"=="Windows_NT" ENDLOCAL
  169.