Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for isdate.bat

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

  1. @ECHO OFF
  2. IF NOT "%OS%"=="Windows_NT" GOTO Syntax
  3.  
  4. SETLOCAL ENABLEDELAYEDEXPANSION
  5.  
  6. IF     "%~1"=="" CALL :Syntax Specify a date in YYYYMMDD or YYYY-MM-DD format
  7. IF NOT "%~2"=="" CALL :Syntax Too many command line arguments
  8.  
  9. :: Specified date must be in YYYYMMDD or YYYY-MM-DD format
  10. ECHO.%~1| FINDSTR /R /X /C:"20[0-9][0-9]-*[01][0-9]-*[0-3][0-9]" >NUL
  11. IF ERRORLEVEL 1 CALL :Invalid "%~1"
  12. SET LocalDate=%~1
  13. SET LocalDate=%LocalDate:-=%
  14.  
  15. :: Extract and verify year
  16. SET Year=%LocalDate:~0,4%
  17. ECHO %Year%| FINDSTR /R /X /C:"20[0-9][0-9]" >NUL || CALL :Invalid "%~1"
  18.  
  19. :: Extract and verify month
  20. SET Month=%LocalDate:~4,2%
  21. ECHO %Month%| FINDSTR /R /X /C:"[01][0-9]" >NUL || CALL :Invalid "%~1"
  22. SET /A Month = 1%Month% - 100
  23. IF %Month% EQU  0 CALL :Invalid "%~1"
  24. IF %Month% GTR 12 CALL :Invalid "%~1"
  25.  
  26. :: Extract and verify day
  27. SET Day=%LocalDate:~6,2%
  28. ECHO %Day%| FINDSTR /R /X /C:"[0-3][0-9]" >NUL || CALL :Invalid "%~1"
  29. SET /A Day = 1%Day% - 100
  30. IF %Day% EQU  0 CALL :Invalid "%~1"
  31. IF %Day% GTR 31 CALL :Invalid "%~1"
  32.  
  33. :: Test for leap year
  34. SET Leapyear=0
  35. SET /A "Leaptest = %Year% %% 4"
  36. IF %Leaptest% EQU 0 SET LeapYear=1
  37. SET /A "Leaptest = %Year% %% 100"
  38. IF %Leaptest% EQU 0 SET LeapYear=0
  39. SET /A "Leaptest = %Year% %% 400"
  40. IF %Leaptest% EQU 0 SET LeapYear=1
  41.  
  42. :: Array with days per month
  43. SET MaxDay.1=31
  44. SET /A Maxday.2 = 28 + %LeapYear%
  45. SET MaxDay.3=31
  46. SET MaxDay.4=30
  47. SET MaxDay.5=31
  48. SET MaxDay.6=30
  49. SET MaxDay.7=31
  50. SET MaxDay.8=31
  51. SET MaxDay.9=30
  52. SET MaxDay.10=31
  53. SET MaxDay.11=30
  54. SET MaxDay.12=31
  55.  
  56. :: Number of days for specified or current months
  57. SET MaxDays=!MaxDay.%Month%!
  58. IF %Day% GTR %MaxDays% CALL :Invalid "%~1"
  59.  
  60. :: If you made it to here, the date is assumed valid
  61. ECHO %~1 is a valid date
  62. ENDLOCAL
  63. EXIT 0
  64.  
  65.  
  66. :Invalid
  67. ECHO %~1 is NOT a valid date
  68.  
  69. :: To abort the entire script we CANNOT use EXIT's /B switch; if
  70. :: the /B switch were used, the batch file would continue after
  71. :: displaying this help text, which we certainly do not want.
  72. ENDLOCAL
  73. EXIT 2
  74.  
  75.  
  76. :Syntax
  77. IF NOT "%*"=="" ECHO.
  78. IF NOT "%*"=="" ECHO ERROR: %*
  79. ECHO.
  80. ECHO IsDate.bat,  Version 1.02 for Windows 2000 and later
  81. ECHO Check if the specified date is a valid date
  82. ECHO.
  83. ECHO Usage:  ISDATE  YYYYMMDD
  84. ECHO    or:  ISDATE  YYYY-MM-DD
  85. ECHO.
  86. ECHO Notes:  Displays the result on screen, and returns 'errorlevel' 0 if the
  87. ECHO         date is valid, or 2 if not (1 is reserved for command line errors).
  88. ECHO.
  89. ECHO Written by Rob van der Woude
  90. ECHO http://www.robvanderwoude.com"
  91.  
  92. :: To abort the entire script we CANNOT use EXIT's /B switch; if
  93. :: the /B switch were used, the batch file would continue after
  94. :: displaying this help text, which we certainly do not want.
  95. IF "%OS%"=="Windows_NT" ENDLOCAL
  96. IF "%OS%"=="Windows_NT" EXIT 1
  97.  

page last modified: 2024-04-16; loaded in 0.0075 seconds