Rob van der Woude's Scripting Pages
Powered by GeSHi

Source code for datetime.bat

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

  1. @ECHO OFF
  2. :: Batch file for Windows XP
  3. :: Written by Justin
  4. IF NOT "%~1"=="" (
  5. 	ECHO No arguments are required "%~nx0" will leave
  6. 	ECHO an errorlevel set in the following format: yyyymmdd
  7. 	EXIT /B
  8. )
  9.  
  10. SETLOCAL ENABLEDELAYEDEXPANSION
  11. FOR /F "skip=4 tokens=3" %%A IN ('REG Query "HKEY_CURRENT_USER\Control Panel\International" /v sDate') DO SET lim=%%A
  12. FOR /F "skip=4 tokens=2,*" %%A IN ('REG Query "HKEY_CURRENT_USER\Control Panel\International" /v sShortDate') DO (
  13. 	SET sdf=%%B
  14. 	SET now=!date!
  15. 	IF DEFINED lim (
  16. 		FOR /F %%D IN ("!lim!") DO (
  17. 			SET sdf=!sdf:%%~D= !
  18. 			SET now=!date:%%~D= !
  19. 		)
  20. 	)
  21. )
  22. FOR %%A IN ("jan=1" "feb=2" "mar=3" "apr=4" "may=5" "jun=6" "jul=7" "aug=8" "sep=9" "oct=10" "nov=11" "dec=12") DO SET now=!now:%%~A!
  23. FOR %%A IN (m o n t u e w d h r f i s a) DO SET now=!now:%%A=!
  24. FOR %%A IN (%sdf%) DO (
  25. 	SET tester=%%A
  26. 	IF "!tester:ddd=!"=="!tester!" (
  27. 		IF NOT "!tester:d=!"=="!tester!" (
  28. 			SET ndf=!ndf! tday
  29. 		) ELSE (
  30. 			IF NOT "!tester:m=!"=="!tester!" (
  31. 				SET ndf=!ndf! tmonth
  32. 			) ELSE (
  33. 				SET ndf=!ndf! tyear
  34. 			)
  35. 		)
  36. 	)
  37. )
  38. CALL :Match %now%
  39. FOR %%A IN (tyear tmonth tday) DO IF NOT DEFINED %%A (
  40. 	>&2 ECHO An Error Occured - Check if it is EVEN POSSIBLE to work out what
  41. 	>&2 ECHO the date is from the %%date%% variable^("%date%"^).
  42. 	ENDLOCAL
  43. 	EXIT /B 1
  44. )
  45. IF %tyear% LSS 99 SET tyear=20%tyear%
  46. IF NOT "%tmonth:~0,1%"=="0" IF %tmonth% LSS 10 SET tmonth=0%tmonth%
  47. IF NOT "%tday:~0,1%"=="0" IF %tday% LSS 10 SET tday=0%tday%
  48. ENDLOCAL & EXIT /B %tyear%%tmonth%%tday%
  49.  
  50.  
  51. :Match
  52. FOR %%A IN (%ndf%) DO (
  53. 	CALL SET %%A=%%1
  54. 	SHIFT
  55. )
  56.  

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