Warning: | Most of the following NT batch files were created and tested in Windows NT 4 and/or Windows 2000. Most of them will work or can be adapted to work in Windows XP as well. However, keep in mind that Microsoft has made some changes in the output of the DATE /T and TIME /T commands, depending on the Regional Settings used.In the Canadian English version of XP for example, DATE /T does not return the Day Of Week, only MM/DD/YY.Workaround: FOR %%A IN (%Date%) DO SET Today=%%A DIR 's time stamp uses HH:mm AM or PM in XP, instead of 2000's HH:mma or m.Workaround: That's a long story, just take a look at SortTime.bat to see how Harry Teufel and myself solved this. And last but not least, REG.EXE 2.0 (native in Windows XP), is not backwards compatible! This means that code written for earlier versions (i.e. the one from the Windows NT 4 Resource Kit) has to be modified. Sometimes you don't need to check for REG.EXE's version, just run the command twice, once for the old REG and once for the new one. This technique is demonstrated in iDate.bat, sDate.bat, iTime.bat and sTime.bat. |
DATEFMT mm
, or the sorted date using DATEFMT yy mm dd /LZ
(/LZ adds leading zeroes to numbers less than 10).DATEFMT mm dd yy / /LZ
(the single forward slash is the specified delimiter).VER | DATE
The current date is: 11/29/2007
Enter the new date: (mm/dd/yy)
DatePart( )
function.In Windows NT, assigning the current time and date to variables is fairly easy to do:
@ECHO OFF FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET Today=%%B ECHO It's %Today% today
@ECHO OFF FOR /F "TOKENS=*" %%A IN ('TIME/T') DO SET Now=%%A ECHO It's %Now% now
However, the actual values are still language dependent.
There is only one way to make them independent of both language and Control Panel's "Regional Settings", and that is by finding out what Control Panel's "Regional Settings" are.
To do that, we need to read the registry entries iDate, sDate, iTime and sTime in HKEY_CURRENT_USER\Control Panel\International.
The following batch files use REG.EXE from the Microsoft ® Windows NT ® Server 4.0 Resource Kit to read the registry settings:
This batch file checks the date format setting (mm/dd/yy, dd/mm/yy or yy/mm/dd):
@ECHO OFF :: For REG.EXE 3.0 (Windows XP) and later versions FOR /F "tokens=3" %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International" /v iDate 2^>NUL') DO SET iDate=%%A :: For earlier REG.EXE versions FOR /F "tokens=3" %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International\iDate" 2^>NUL') DO SET iDate=%%A ECHO HKEY_CURRENT_USER\Control Panel\International\iDate=%iDate%
This batch file checks the date separator used:
@ECHO OFF :: For REG.EXE 3.0 (Windows XP) and later versions FOR /F "tokens=3" %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International" /v sDate 2^>NUL') DO SET sDate=%%A :: For earlier REG.EXE versions FOR /F "tokens=3" %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International\sDate" 2^>NUL') DO SET sDate=%%A ECHO HKEY_CURRENT_USER\Control Panel\International\sDate=%sDate%
This batch file checks the time format setting (12 or 24 hour clock):
@ECHO OFF :: For REG.EXE 3.0 (Windows XP) and later versions FOR /F "tokens=3" %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International" /v iTime 2^>NUL') DO SET iTime=%%A :: For earlier REG.EXE versions FOR /F "tokens=3" %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International\iTime" 2^>NUL') DO SET iTime=%%A ECHO HKEY_CURRENT_USER\Control Panel\International\iTime=%iTime%
This batch file checks the time separator used:
@ECHO OFF :: For REG.EXE 3.0 (Windows XP) and later versions FOR /F "tokens=3" %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International" /v sTime 2^>NUL') DO SET sTime=%%A :: For earlier REG.EXE versions FOR /F "tokens=3" %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International\sTime" 2^>NUL') DO SET sTime=%%A ECHO HKEY_CURRENT_USER\Control Panel\International\sTime=%sTime%
Now that we know Control Panel's "Regional Settings", we can use them to set language and Control Panel independent date and time variables:
This batch file sets a variable SORTDATE which can be used for sorting.
Version 2.* uses REG.EXE from the Microsoft ® Windows NT ® Server 4.0 Resource Kit to read the registry.
Note: | This makes it incompatible with Windows 2000's or later versions of REG.EXE. It is possible to fix the code, as shown in iDate, sDate, iTime and sTime, but for XP one of the later version is better suited. |
@ECHO OFF :: SortDate, Version 2.00 for Windows NT :: Save date and "sorted" date in environment variables. :: :: This batch file will always display the same results, :: independent of "International" settings. :: This batch file uses REG.EXE from the NT Resource Kit :: to read the "International" settings from the registry. :: :: Written by Rob van der Woude :: http://www.robvanderwoude.com FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET DATE=%%B :: Delims is a TAB followed by a space FOR /F "TOKENS=2* DELIMS= " %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International\iDate"') DO SET iDate=%%B FOR /F "TOKENS=2* DELIMS= " %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International\sDate"') DO SET sDate=%%B IF %iDate%==0 FOR /F "TOKENS=1-4* DELIMS=%sDate% " %%A IN ('DATE/T') DO SET SortDate=%%D%%B%%C IF %iDate%==1 FOR /F "TOKENS=1-4* DELIMS=%sDate% " %%A IN ('DATE/T') DO SET SortDate=%%D%%C%%B IF %iDate%==2 FOR /F "TOKENS=1-4* DELIMS=%sDate% " %%A IN ('DATE/T') DO SET SortDate=%%B%%C%%D ECHO It's %DATE% today ECHO For sorting purposes: %SortDate%
Version 3.* uses NT's native REGEDIT.EXE to read the registry:
@ECHO OFF :: Windows NT 4 or later only IF NOT "%OS%"=="Windows_NT" GOTO Syntax :: No command line arguments required IF NOT [%1]==[] GOTO Syntax :: Keep variables local SETLOCAL :: Store current date in a variable in default Locale format FOR /F "TOKENS=1* DELIMS= " %%A IN ('DATE/T') DO SET DATE=%%B ECHO It is %DATE% today :: Store current date in a variable in YYYYMMDD format :: Export registry settings to a temporary file START /W REGEDIT /E %TEMP%.\_TEMP.REG "HKEY_CURRENT_USER\Control Panel\International" :: Read the exported data FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "iDate"') DO SET iDate=%%B FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "sDate"') DO SET sDate=%%B DEL %TEMP%.\_TEMP.REG :: Remove quotes SET iDate=%iDate:"=% SET sDate=%sDate:"=% :: Parse today's date depending on registry's date format settings IF %iDate%==0 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO ( SET Year=%%C SET Month=%%A SET Day=%%B ) IF %iDate%==1 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO ( SET Year=%%C SET Month=%%B SET Day=%%A ) IF %iDate%==2 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO ( SET Year=%%A SET Month=%%B SET Day=%%C ) :: Remove the day of week if applicable FOR %%A IN (%Year%) DO SET Year=%%A FOR %%A IN (%Month%) DO SET Month=%%A FOR %%A IN (%Day%) DO SET Day=%%A :: Today's date in YYYYMMDD format SET SortDate=%Year%%Month%%Day% ECHO For sorting purposes: %SortDate% :: Done ENDLOCAL & SET SortDate=%SortDate% GOTO:EOF :Syntax ECHO. ECHO SortDate, Version 3.10 for Windows NT 4 / 2000 / XP ECHO Save date and "sorted" date in environment variables. ECHO. ECHO Usage: %0 ECHO. ECHO This batch file will always display the same results, ECHO independent of "International" settings. ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com ECHO Adapted for Windows XP with help from Kailash Chanduka
Version 4.* uses internal commands only.
It was based on
Simon Sheppard's
GetDate.bat,
and modified by me to make it not only independent of Regional Settings,
but less dependent on the language as well.
@ECHO OFF CLS ECHO. ECHO SortDate.bat, Version 4.00 for Windows NT 4 / 2000 / XP ECHO Display day, month and year, independent of Windows' ECHO Regional Settings, and using internal commands only. ECHO. ECHO Based on Simon Sheppard's GetDate.bat ECHO http://ss64.com/ntsyntax/GetDate.txt ECHO. ECHO This version demonstrates how to adjust the code for non-English Windows ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com ECHO. SETLOCAL :: Get the format: dd-mm-yy, or mm/dd/yy, or whatever other format your PC uses FOR /F "tokens=2 delims=()" %%A IN ('ECHO. ^| DATE') DO SET Format=%%A :: Get the delimiter used: the first character that is different SET Char1=%Format:~0,1% SET Char2=%Format:~1,1% SET Char3=%Format:~2,1% SET Char4=%Format:~3,1% SET Char5=%Format:~4,1% IF NOT [%Char1%]==[%Char2%] ( SET Delim=%Char2% GOTO Parse ) IF NOT [%Char2%]==[%Char3%] ( SET Delim=%Char3% GOTO Parse ) IF NOT [%Char3%]==[%Char4%] ( SET Delim=%Char4% GOTO Parse ) IF NOT [%Char4%]==[%Char5%] ( SET Delim=%Char5% ) ELSE ( ECHO Error finding delimiter. ECHO Aborting . . . GOTO:EOF ) :: Get the current date string :Parse FOR /F "tokens=1* delims= " %%A IN ('DATE/T') DO IF "%%B"=="" (SET Date=%%A) ELSE (SET Date=%%B) :: Display the intermediate results ECHO Date=%Date% Format=%Format% Delim=%Delim% ECHO. :: Parse the Date string using the delimiter found earlier FOR %%? IN (1,2,3) DO CALL :ParseVal %%? :: This assumes the variable names are dd, mm and yy ECHO. ECHO For English NT versions only, modify if format does NOT use dd, mm and yy. ECHO SET SortDate=%%yy%%%%mm%%%%dd%% ECHO. SET SortDate=%yy%%mm%%dd% SET SortDate ENDLOCAL & SET SortDate=%SortDate% GOTO:EOF :ParseVal :: Get the day, month or year variable name FOR /F "tokens=%1 delims=%Delim% " %%A IN ('ECHO.%Format%') DO SET Var%1=%%A :: Get the day, month or year variable value FOR /F "tokens=%1 delims=%Delim% " %%A IN ('ECHO.%Date%') DO SET Val%1=%%A :: Assingn the value CALL SET %%Var%1%%=%%Val%1%% :: Display the result CALL SET %%Var%1%% GOTO:EOF
Version 5.* uses Windows XP Professional's
WMIC command.
It is not only independent of Regional Settings, but language independent as well.
@ECHO OFF ECHO. :: Check Windows version (XP Pro or later) and command line arguments (none) IF NOT "%OS%"=="Windows_NT" GOTO Syntax IF NOT "%~1"=="" GOTO Syntax WMIC.EXE Alias /? >NUL 2>&1 || GOTO Syntax SETLOCAL ENABLEDELAYEDEXPANSION :: Use WMIC to retrieve date and time FOR /F "skip=1 tokens=1-6" %%A IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO ( IF NOT "%%~F"=="" ( SET /A SortDate = 10000 * %%F + 100 * %%D + %%A SET /A SortTime = 10000 * %%B + 100 * %%C + %%E SET SortTime=0000000!SortTime! SET SortTime=!SortTime:~-6! ) ) :: Display the results: SET Sort :: Done ENDLOCAL GOTO:EOF :Syntax ECHO SortDate.bat, Version 5.01 for Windows XP Pro and later ECHO Displays date and time in YYYYMMDD and HHmmSS format, ECHO completely independent of Windows' Regional Settings or ECHO language, and without the use of temporary files. ECHO. ECHO Usage: SORTDATE ECHO. ECHO Note: Uses WMIC to query the current date and time; ECHO WMIC is native in Windows XP Professional, ECHO Windows Server 2003 and Windows Vista. ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com ECHO.
This batch file sets a variable SORTTIME which can be used for sorting.
Version 2.* uses REG.EXE from the Microsoft ® Windows NT ® Server 4.0 Resource Kit to read the registry:
Note: | This makes it incompatible with Windows XP's (or Vista's) version of REG.EXE. It is possible to fix the code, as shown in iDate, sDate, iTime and sTime, but for XP one of the later version is better suited. |
@ECHO OFF :: SortTime, Version 2.10 for Windows NT :: Save time and "sorted" time in environment variables. :: :: This batch file will always display the same results, :: independent of "International" settings. :: This batch file uses REG.EXE from the NT Resource Kit :: to read the "International" settings from the registry. :: :: Written by Rob van der Woude :: http://www.robvanderwoude.com FOR /F "TOKENS=*" %%A IN ('TIME/T') DO SET TIME=%%A :: Delims is a TAB followed by a space FOR /F "TOKENS=2* DELIMS= " %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International\iTime"') DO SET iTime=%%B FOR /F "TOKENS=2* DELIMS= " %%A IN ('REG QUERY "HKEY_CURRENT_USER\Control Panel\International\sTime"') DO SET sTime=%%B IF %iTime%==1 GOTO Sort SET AMPMTIME=%TIME% FOR /F "TOKENS=1,2* DELIMS=%sTime% " %%A IN ('ECHO %TIME%') DO ( SET HOUR=%%A SET MINS=%%B ) SET AMPM=%MINS:~2,1% SET MINS=%MINS:~0,2% IF %HOUR% LSS 12 IF /I %AMPM%==P SET /A HOUR=%HOUR%+12 IF %HOUR% LSS 10 IF /I %AMPM%==A SET HOUR=0%HOUR% IF %HOUR% EQU 12 IF /I %AMPM%==A SET HOUR=00 SET TIME=%HOUR%%sTime%%MINS% :Sort FOR /F "TOKENS=1,2* DELIMS=%sTime% " %%A IN ('ECHO %TIME%') DO SET SORTTIME=%%A%%B ECHO It's %TIME% now :: Thanks for Holger Stein who mailed me this correction (add leading zero): IF %SORTTIME% LSS 1000 SET SORTTIME=0%SORTTIME% ECHO For sorting purposes: %SORTTIME%
Version 3.12 uses NT's native REGEDIT.EXE to read the registry:
@ECHO OFF :: SortTime, Version 3.12 for Windows NT 4 and 2000 :: Save time and "sorted" time in environment variables. :: :: This batch file will always display the same results, :: independent of "International" settings. :: :: Written by Rob van der Woude :: http://www.robvanderwoude.com :: Help required? IF NOT [%1]==[] GOTO Syntax :: Windows NT 4/2000 only SET OS_OK=0 VER | FIND "Windows NT" >NUL IF NOT ERRORLEVEL 1 SET OS_OK=1 VER | FIND "Windows 2000" >NUL IF NOT ERRORLEVEL 1 SET OS_OK=1 IF [%OS_OK%]==[0] GOTO :OsErr :: Command Extensions should be enabled SET /A 12 + 4 2>&1 | FIND "16" >NUL IF ERRORLEVEL 1 GOTO :CmdExtErr :: Store current time in default Locale format FOR /F "TOKENS=*" %%A IN ('TIME/T') DO SET TIME=%%A ECHO It's %TIME% now :: Store current time in a variable in hh:mm format :: Export registry settings to a temporary file START /W REGEDIT /E %TEMP%.\_TEMP.REG "HKEY_CURRENT_USER\Control Panel\International" :: Read the exported data, filtering out keys added by Windows 2000 FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND "iTime" ^| FIND /V "iTimePrefix"') DO SET iTime=%%B :: Thanks for Daniel R. Foster for correcting a typo in the next line FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND "sTime" ^| FIND /V "sTimeFormat"') DO SET sTime=%%B DEL %TEMP%.\_TEMP.REG :: Remove quotes SET iTime=%iTime:"=% SET sTime=%sTime:"=% :: Format SORTTIME depending on registry settings IF %iTime%==1 GOTO Sort SET AMPMTIME=%TIME% FOR /F "TOKENS=1,2* DELIMS=%sTime% " %%A IN ('ECHO %TIME%') DO ( SET HOUR=%%A SET MINS=%%B ) SET AMPM=%MINS:~2,1% SET MINS=%MINS:~0,2% IF %HOUR% LSS 12 IF /I %AMPM%==P SET /A HOUR=%HOUR%+12 IF %HOUR% LSS 10 IF /I %AMPM%==A SET HOUR=0%HOUR% IF %HOUR% EQU 12 IF /I %AMPM%==A SET HOUR=00 SET TIME=%HOUR%%sTime%%MINS% :Sort FOR /F "TOKENS=1,2* DELIMS=%sTime% " %%A IN ('ECHO %TIME%') DO SET SORTTIME=%%A%%B :: Thanks for Holger Stein who mailed me this correction (add leading zero): IF %SORTTIME% LSS 1000 SET SORTTIME=0%SORTTIME% ECHO For sorting purposes: %SORTTIME% GOTO End :CmdExtErr ECHO. ECHO Command extensions need to be enabled for this batch file to run correctly. ECHO. ECHO You can run this batch file using the command: ECHO. ECHO CMD /X %~n0 ECHO. ECHO to enable command extensions, however, the results will not be saved in ECHO environment variables that way. GOTO Syntax :OsErr ECHO. ECHO This batch file is written for Windows NT 4 and Windows 2000 only! ECHO. ECHO Visit http://www.robvanderwoude.com for Kix and Rexx versions. :Syntax ECHO. ECHO SortTime.bat, Version 3.12 for Windows NT 4 and Windows 2000 ECHO Displays the time of execution in the system's default time format and in ECHO hhmm format for sorting purposes. ECHO The values are also stored in the environment variables DATE and SORTDATE. ECHO This version uses native Windows commands only. ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com ECHO. ECHO Usage: %~n0 :End SET OS_OK=
Version 3.40 uses FINDSTR to simplify filtering values from the registry.
FINDSTR is native as of Windows 2000. For NT 4 use FINDSTR from the Resource Kit.
Due to differences in AM/PM notations brute force had to be used to split the minutes from the AM/PM text.
@ECHO OFF :: Windows 2000 and later only IF NOT "%OS%"=="Windows_NT" GOTO OsErr :: Remove the following line to use this batch file :: in Windows NT 4 with the Resource Kit installed VER | FIND "Windows NT" >NUL && GOTO OsErr :: Help required? IF NOT "%~1"=="" GOTO Syntax :: Enable Command Extensions and use local variables VERIFY OTHER 2>nul SETLOCAL ENABLEEXTENSIONS IF ERRORLEVEL 1 GOTO CmdExtErr :: Export registry settings to a temporary file START /W REGEDIT /E "%Temp%.\_SortTime.reg" "HKEY_CURRENT_USER\Control Panel\International" :: Read the time format from the exported data FOR /F "tokens=1* delims==" %%A IN ('TYPE "%Temp%.\_SortTime.reg" ^| FINDSTR /R /I /B /C:"\"iTime\"="') DO SET iTime=%%~B :: Thanks for Daniel R. Foster for correcting a typo in the next line FOR /F "tokens=1* delims==" %%A IN ('TYPE "%Temp%.\_SortTime.reg" ^| FINDSTR /R /I /B /C:"\"sTime\"="') DO SET sTime=%%~B DEL "%Temp%.\_SortTime.reg" :: Store current time in default Locale format FOR /F "tokens=*" %%A IN ('TIME/T') DO SET Now=%%A ECHO It's %Now% now :: Format SortTime depending on registry settings FOR /F "tokens=1,2* delims=%sTime% " %%A IN ('ECHO %Now%') DO ( SET Hour=%%A SET Mins=%%B ) :: Translate AM/PM to 24 hours format; :: A correction was required for Windows XP, :: thanks for Harry Teufel for finding this bug ECHO.%Mins%| FIND /I "AM" >NUL && SET AmPm=A ECHO.%Mins%| FIND /I "PM" >NUL && SET AmPm=P :: AM/PM can be all upper or mixed case, with or without leading space; we'll just erase all possible characters SET Mins=%Mins: =% SET Mins=%Mins:A=% SET Mins=%Mins:a=% SET Mins=%Mins:M=% SET Mins=%Mins:m=% SET Mins=%Mins:P=% SET Mins=%Mins:p=% IF 1%Hour% LSS 20 SET Hour=0%Hour% IF 1%Mins% LSS 20 SET Mins=0%Mins% IF %Hour% LSS 12 IF /I "%AmPm%"=="P" SET /A Hour=1%Hour%-88 IF %Hour% EQU 12 IF /I "%AmPm%"=="A" SET Hour=00 SET Now=%Hour%%sTime%%Mins% :Sort FOR /F "tokens=1,2* delims=%sTime% " %%A IN ('ECHO %Now%') DO SET SortTime=%%A%%B :: Thanks for Holger Stein who mailed me this correction (add leading zero): IF %SortTime% LSS 1000 SET SortTime=0%SortTime% ECHO For sorting purposes: %SortTime% :: Purge local variables except SortTime ENDLOCAL & SET SortTime=%SortTime% GOTO End :CmdExtErr ECHO. ECHO Command extensions need to be enabled for this batch file to run correctly. ECHO. ECHO You can run this batch file using the command: ECHO. ECHO CMD /X %~n0 ECHO. ECHO to enable command extensions, however, the results will not be saved in ECHO environment variables that way. GOTO Syntax :OsErr ECHO. ECHO This batch file requires Windows 2000 or a later version! ECHO. ECHO To use this batch file with Windows NT 4 you need to install FINDSTR from ECHO the Resource Kit, and to remove the NT 4 check from this batch file. ECHO Read the comment lines in this batch file for details. :Syntax ECHO. ECHO SortTime.bat, Version 3.40 for Windows 2000 / XP ECHO Displays the time of execution in the system's default time format and in ECHO hhmm format for sorting purposes. ECHO. ECHO Usage: SORTTIME ECHO. ECHO Notes: [1] The sorted value is stored in the environment variable SORTTIME. ECHO The SORTTIME value is independent of "International", "Regional" ECHO or "Locale" settings. ECHO [2] This batch file uses native Windows 2000 commands only. ECHO To allow the use in Windows NT 4, make sure FINDSTR from the ECHO Resource Kit is installed, and remove the NT 4 check from this ECHO batch file. Read the comment lines for details. ECHO [3] KiXtart, Perl, JScript, Rexx and VBScript versions are available ECHO at http://www.robvanderwoude.com/datetimenonbatch.html ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com ECHO ^(with corrections by Daniel R. Foster, Harry Teufel and Holger Stein^) ECHO. :End :: Done
If you have 100% certainty that you will have REG.EXE available, use Version 2.*, otherwise use Version 3.*.
Version 2.* doesn't need to write to a temporary file.
Version 3.12 can be used in NT 4 without modification.
Version 3.20 needs to be modified to be used with NT 4,and in that case FINDSTR.EXE from the Resource Kit. needs to be installed.
The code from version 3 of SORTTIME.BAT was also used in AtFuture.bat, a coproduction with Rob Fuller (click here to download the ZIPped source).
@ECHO OFF ECHO. :: Check the Windows version IF NOT "%OS%"=="Windows_NT" GOTO Syntax SETLOCAL :: Initialize variable SET Error=0 :: Check the command line arguments IF "%~1"=="" GOTO Syntax IF NOT "%~3"=="" GOTO Syntax IF "%~2"=="" ( FOR %%A IN (%Date%) DO SET cDate=%%A SET cDays=%~1 ) ELSE ( SET cDate=%~1 SET cDays=%~2 ) :: Read the Date format from the registry CALL :ReadDateFormat :: Check if a valid date was specified (ECHO.%cDate%) | FINDSTR /R /B /C:"[0-9]*\%sDate%[0-9]*\%sDate%[0-9]*" >NUL IF ERRORLEVEL 1 ( ECHO Error: %cDate% is not a valid date ECHO. GOTO Syntax ) :: Check if the second argument is a valid number (ECHO.%cDays%) | FINDSTR /R /B /C:"-*[0-9]*" >NUL IF ERRORLEVEL 1 ( ECHO Error: %cDays% is not an integer ECHO. GOTO Syntax ) :: Parse the date specified CALL :ParseDate %cDate% :: Check for errors IF %Error% NEQ 0 GOTO Syntax :: Convert the parsed Gregorian date to Julian CALL :JDate %GYear% %GMonth% %GDay% :: Display original input ECHO Starting date : %cDate% :: Add or subtract the specified number of days IF "%cDays:~0,1%"=="-" ( SET /A NewJDate = %JDate% - %cDays:~1% ECHO Days subtracted : %cDays:~1% ) ELSE ( SET /A NewJDate = %JDate% + %cDays% ECHO Days added : %cDays% ) :: Convert the new Julian date back to Gregorian again CALL :GDate %NewJDate% :: Reformat the date to local format CALL :ReformatDate %GDate% :: Display the result ECHO Resulting date : %LDate% :: Return the result in a variable named after this batch file ENDLOCAL & SET %~n0=%LDate% GOTO:EOF ::===================================:: :: :: :: - S u b r o u t i n e s - :: :: :: ::===================================:: :GDate :: Convert Julian date back to "normal" Gregorian date :: Argument : Julian date :: Returns : YYYY MM DD :: :: Algorithm based on Fliegel-Van Flandern :: algorithm from the Astronomical Almanac, :: provided by Doctor Fenton on the Math Forum :: (http://mathforum.org/library/drmath/view/51907.html), :: and converted to batch code by Ron Bakowski. :: SET /A P = %1 + 68569 SET /A Q = 4 * %P% / 146097 SET /A R = %P% - ( 146097 * %Q% +3 ) / 4 SET /A S = 4000 * ( %R% + 1 ) / 1461001 SET /A T = %R% - 1461 * %S% / 4 + 31 SET /A U = 80 * %T% / 2447 SET /A V = %U% / 11 SET /A GYear = 100 * ( %Q% - 49 ) + %S% + %V% SET /A GMonth = %U% + 2 - 12 * %V% SET /A GDay = %T% - 2447 * %U% / 80 :: Clean up the mess FOR %%A IN (P Q R S T U V) DO SET %%A= :: Add leading zeroes IF 1%GMonth% LSS 20 SET GMonth=0%GMonth% IF 1%GDay% LSS 20 SET GDay=0%GDay% :: Return value SET GDate=%GYear% %GMonth% %GDay% GOTO:EOF :JDate :: Convert date to Julian :: Arguments : YYYY MM DD :: Returns : Julian date :: :: First strip leading zeroes SET MM=%2 SET DD=%3 IF %MM:~0,1% EQU 0 SET MM=%MM:~1% IF %DD:~0,1% EQU 0 SET DD=%DD:~1% :: :: Algorithm based on Fliegel-Van Flandern :: algorithm from the Astronomical Almanac, :: provided by Doctor Fenton on the Math Forum :: (http://mathforum.org/library/drmath/view/51907.html), :: and converted to batch code by Ron Bakowski. SET /A Month1 = ( %MM% - 14 ) / 12 SET /A Year1 = %1 + 4800 SET /A JDate = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %MM% - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %DD% - 32075 FOR %%A IN (Month1 Year1) DO SET %%A= GOTO:EOF :ParseDate :: Parse (Gregorian) date depending on registry's date format settings :: Argument : Gregorian date in local date format, :: Requires : sDate (local date separator), iDate (local date format number) :: Returns : GYear (4-digit year), GMonth (2-digit month), GDay (2-digit day) :: IF %iDate%==0 FOR /F "TOKENS=1-3 DELIMS=%sDate%" %%A IN ('ECHO.%1') DO ( SET GYear=%%C SET GMonth=%%A SET GDay=%%B ) IF %iDate%==1 FOR /F "TOKENS=1-3 DELIMS=%sDate%" %%A IN ('ECHO.%1') DO ( SET GYear=%%C SET GMonth=%%B SET GDay=%%A ) IF %iDate%==2 FOR /F "TOKENS=1-3 DELIMS=%sDate%" %%A IN ('ECHO.%1') DO ( SET GYear=%%A SET GMonth=%%B SET GDay=%%C ) IF %GDay% GTR 31 SET Error=1 IF %GMonth% GTR 12 SET Error=1 GOTO:EOF :ReadDateFormat :: Read the Date format from the registry. :: Arguments : none :: Returns : sDate (separator), iDate (date format number) :: :: First, export registry settings to a temporary file: START /W REGEDIT /E "%TEMP%.\_TEMP.REG" "HKEY_CURRENT_USER\Control Panel\International" :: Now, read the exported data: FOR /F "tokens=1* delims==" %%A IN ('TYPE "%TEMP%.\_TEMP.REG" ^| FIND /I "iDate"') DO SET iDate=%%B FOR /F "tokens=1* delims==" %%A IN ('TYPE "%TEMP%.\_TEMP.REG" ^| FIND /I "sDate"') DO SET sDate=%%B :: Remove the temporary file: DEL "%TEMP%.\_TEMP.REG" :: Remove quotes from the data read: :: SET iDate=%iDate:"=% FOR %%A IN (%iDate%) DO SET iDate=%%~A :: SET sDate=%sDate:"=% FOR %%A IN (%sDate%) DO SET sDate=%%~A GOTO:EOF :ReformatDate :: Reformat the date back to the local format :: Arguments : YYYY MM DD :: Returns : LDate (Gregorian date in local format) :: IF %iDate%==0 SET LDate=%2%sDate%%3%sDate%%1 IF %iDate%==1 SET LDate=%3%sDate%%2%sDate%%1 IF %iDate%==2 SET LDate=%1%sDate%%2%sDate%%3 GOTO:EOF :Syntax ECHO DateAdd.bat, Version 1.10 for Windows NT 4 / 2000 / XP / Server 2003 / Vista ECHO Add (or subtract) the specified number of days to (or from) the specified date ECHO. ECHO Usage: DATEADD [ date ] days ECHO. ECHO Where: "date" is a "normal" Gregorian date in the local computer's format ECHO (default value if no date is specified: today's date) ECHO "days" is the number of days to add or subtract ECHO. IF "%OS%"=="Windows_NT" FOR %%A IN (%Date%) DO SET Today=%%A IF "%OS%"=="Windows_NT" ECHO E.g. DATEADD %Today% 1 will return tomorrow's date (as will DATEADD 1) IF "%OS%"=="Windows_NT" ECHO DATEADD %Today% -1 will return yesterday's date (as will DATEADD -1) IF "%OS%"=="Windows_NT" ENDLOCAL IF NOT "%OS%"=="Windows_NT" ECHO E.g. DATEADD 01/25/2007 1 should return 01/26/2007 IF NOT "%OS%"=="Windows_NT" ECHO DATEADD 01/25/2007 -1 should return 01/24/2007 ECHO. ECHO Julian date conversion based on Fliegel-Van Flandern algorithms from ECHO the Astronomical Almanac, provided by Doctor Fenton on the Math Forum ECHO (http://mathforum.org/library/drmath/view/51907.html), and converted ECHO to batch code by Ron Bakowski. ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com
@ECHO OFF ECHO. :: Check the Windows version IF NOT "%OS%"=="Windows_NT" GOTO Syntax SETLOCAL :: Check the command line arguments IF "%˜1"=="" GOTO Syntax IF NOT "%˜3"=="" GOTO Syntax :: Read the Date format from the registry CALL :ReadDateFormat :: Check if the first date is valid (ECHO.%1) | FINDSTR /R /B /C:"[0-9]*\%sDate%[0-9]*\%sDate%[0-9]*" >NUL IF ERRORLEVEL 1 ( ECHO Error: %1 is not a valid date ECHO. GOTO Syntax ) :: Use today if no second date was specified IF "%˜2"=="" ( FOR %%A IN (%Date%) DO SET Date2=%%A ) ELSE ( SET Date2=%2 ) :: Check if the second date is valid (ECHO.%Date2%) | FINDSTR /R /B /C:"[0-9]*\%sDate%[0-9]*\%sDate%[0-9]*" >NUL IF ERRORLEVEL 1 ( ECHO Error: %Date2% is not a valid date ECHO. GOTO Syntax ) :: Parse the first date CALL :ParseDate %1 :: Convert the parsed Gregorian date to Julian CALL :JDate %GYear% %GMonth% %GDay% :: Save the resulting Julian date SET JDate1=%JDate% :: Parse the second date CALL :ParseDate %Date2% :: Convert the parsed Gregorian date to Julian CALL :JDate %GYear% %GMonth% %GDay% :: Calculate the absolute value of the difference in days IF %JDate% GTR %JDate1% ( SET /A DateDiff = %JDate% - %JDate1% ) ELSE ( SET /A DateDiff = %JDate1% - %JDate% ) :: Format output for singular or plural SET Days=days IF %DateDiff% EQU 1 SET Days=day :: Prefix value with a minus sign if negative IF %JDate% GTR %JDate1% SET DateDiff=-%DateDiff% :: Display the result ECHO First date : %1 ECHO Second date : %Date2% ECHO Difference : %DateDiff% %Days% :: Return the result in a variable named after this batch file ENDLOCAL & SET %˜n0=%DateDiff% GOTO:EOF ::===================================:: :: :: :: - S u b r o u t i n e s - :: :: :: ::===================================:: :JDate :: Convert date to Julian :: Arguments : YYYY MM DD :: Returns : Julian date :: :: First strip leading zeroes; a logical error in this :: routine was corrected with help from Alexander Shapiro SET MM=%2 SET DD=%3 IF 1%MM% LSS 110 SET MM=%MM:˜1% IF 1%DD% LSS 110 SET DD=%DD:˜1% :: :: Algorithm based on Fliegel-Van Flandern :: algorithm from the Astronomical Almanac, :: provided by Doctor Fenton on the Math Forum :: (http://mathforum.org/library/drmath/view/51907.html), :: and converted to batch code by Ron Bakowski. SET /A Month1 = ( %MM% - 14 ) / 12 SET /A Year1 = %1 + 4800 SET /A JDate = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %MM% - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %DD% - 32075 FOR %%A IN (Month1 Year1) DO SET %%A= GOTO:EOF :ParseDate :: Parse (Gregorian) date depending on registry's date format settings :: Argument : Gregorian date in local date format, :: Requires : sDate (local date separator), iDate (local date format number) :: Returns : GYear (4-digit year), GMonth (2-digit month), GDay (2-digit day) :: IF %iDate%==0 FOR /F "TOKENS=1-3 DELIMS=%sDate%" %%A IN ('ECHO.%1') DO ( SET GYear=%%C SET GMonth=%%A SET GDay=%%B ) IF %iDate%==1 FOR /F "TOKENS=1-3 DELIMS=%sDate%" %%A IN ('ECHO.%1') DO ( SET GYear=%%C SET GMonth=%%B SET GDay=%%A ) IF %iDate%==2 FOR /F "TOKENS=1-3 DELIMS=%sDate%" %%A IN ('ECHO.%1') DO ( SET GYear=%%A SET GMonth=%%B SET GDay=%%C ) GOTO:EOF :ReadDateFormat :: Read the Date format from the registry. :: Arguments : none :: Returns : sDate (separator), iDate (date format number) :: :: First, export registry settings to a temporary file: START /W REGEDIT /E "%TEMP%.\_TEMP.REG" "HKEY_CURRENT_USER\Control Panel\International" :: Now, read the exported data: FOR /F "tokens=1* delims==" %%A IN ('TYPE "%TEMP%.\_TEMP.REG" ^| FIND /I "iDate"') DO SET iDate=%%B FOR /F "tokens=1* delims==" %%A IN ('TYPE "%TEMP%.\_TEMP.REG" ^| FIND /I "sDate"') DO SET sDate=%%B :: Remove the temporary file: DEL "%TEMP%.\_TEMP.REG" :: Remove quotes from the data read: :: SET iDate=%iDate:"=% FOR %%A IN (%iDate%) DO SET iDate=%%˜A :: SET sDate=%sDate:"=% FOR %%A IN (%sDate%) DO SET sDate=%%˜A GOTO:EOF :Syntax ECHO DateDiff.bat, Version 1.10 for Windows NT 4 / 2000 / XP / Server 2003 / Vista ECHO Calculate the difference (in days) between two dates ECHO. ECHO Usage: DATEDIFF date [ date ] ECHO. ECHO Where: "date" is a "normal" Gregorian date in the local computer's format; ECHO if no second date is specified, today is assumed ECHO. ECHO Julian date conversion based on Fliegel-Van Flandern algorithms from ECHO the Astronomical Almanac, provided by Doctor Fenton on the Math Forum ECHO (http://mathforum.org/library/drmath/view/51907.html), and converted ECHO to batch code by Ron Bakowski. ECHO Bug found by and converted with help from Alexander Shapiro. ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com IF "%OS%"=="Windows_NT" ENDLOCAL
@ECHO OFF IF NOT "%OS%"=="Windows_NT" GOTO Syntax IF "%˜1"=="" GOTO Syntax IF NOT "%˜6"=="" GOTO Syntax ECHO.%* | FIND "?" > NUL && GOTO Syntax SETLOCAL ENABLEDELAYEDEXPANSION FOR /F "tokens=* delims=" %%A IN ('VER ^| DATE') DO ( REM Save only last word in variable FOR %%B IN (%%A) DO SET DateFmt=%%B FOR /F "tokens=1-3 delims=(/-)" %%B IN ("!DateFmt!") DO ( SET Var1=%%B SET Var2=%%C SET Var3=%%D ) ) FOR /F "tokens=* delims=" %%A IN ('VER ^| DATE ^| FIND /V "("') DO ( REM Save only last word in variable, thereby ignoring optional REM leading day of week, error reported by Fernando Sader FOR %%B IN (%%A) DO SET Today=%%B FOR /F "tokens=1-3 delims=./- " %%B IN ("!Today!") DO ( SET %Var1%=%%B SET %Var2%=%%C SET %Var3%=%%D ) ) IF /I NOT "%˜1"=="%Var1%" IF /I NOT "%˜1"=="%Var2%" IF /I NOT "%˜1"=="%Var3%" ( ENDLOCAL GOTO Syntax ) IF /I "%˜4"=="/LZ" (SET Delim=) ELSE (SET Delim=%4) IF /I NOT "%˜3"=="%Var1%" IF /I NOT "%˜3"=="%Var2%" IF /I NOT "%˜3"=="%Var3%" IF /I NOT "%˜3"=="/LZ" (SET Delim=%˜3) IF /I NOT "%˜2"=="%Var1%" IF /I NOT "%˜2"=="%Var2%" IF /I NOT "%˜2"=="%Var3%" IF /I NOT "%˜2"=="/LZ" (SET Delim=%˜2) ECHO.%* | FIND /I "/LZ" >NUL IF NOT ERRORLEVEL 1 CALL :AddLeadingZero SET DateFmt=!%1! IF /I NOT "%˜2"=="%Delim%" IF /I NOT "%˜2"=="/LZ" (SET DateFmt=%DateFmt%%Delim%!%2!) IF /I NOT "%˜3"=="%Delim%" IF /I NOT "%˜3"=="/LZ" (SET DateFmt=%DateFmt%%Delim%!%3!) ENDLOCAL & SET DateFmt=%DateFmt% SET DateFmt GOTO:EOF :AddLeadingZero CALL SET Char1=%%%Var1%:˜0,1%% IF NOT "%Char1%"=="0" ( IF !%Var1%! LSS 10 SET %Var1%=0!%Var1%! ) CALL SET Char1=%%%Var2%:˜0,1%% IF NOT "%Char1%"=="0" ( IF !%Var2%! LSS 10 SET %Var2%=0!%Var2%! ) CALL SET Char1=%%%Var3%:˜0,1%% IF NOT "%Char1%"=="0" ( IF !%Var3%! LSS 10 SET %Var3%=0!%Var3%! ) GOTO:EOF :Syntax ECHO DateFmt.bat, Version 0.53 BETA for Windows NT 4 and later ECHO Display the current date in the specified format ECHO. ECHO Usage: DATEFMT date_format [ delimiter ] [ /LZ ] ECHO. IF "%OS%"=="Windows_NT" FOR /F "tokens=2-4 delims=()/-" %%A IN ('VER ^| DATE ^| FIND "("') DO ECHO Where: date_format is any combination of %%A, %%B and/or %%C IF NOT "%OS%"=="Windows_NT" ECHO Where: date_format is any combination of dd, mm and/or yy ECHO (these date_format options are always in the computer's IF NOT "%OS%"=="Windows_NT" ECHO local language; to look them up, type VER ³ DATE) IF NOT "%OS%"=="Windows_NT" GOTO Skip ECHO local language; to look them up, type VER ^| DATE) :Skip ECHO delimiter is the delimiter to be used in the end result ECHO /LZ use leading zeroes in the end result ECHO. ECHO Examples (for English Windows versions): ECHO DATEFMT yy mm dd --- 2007115 (January 15 or November 5, 2007) ECHO DATEFMT yy mm dd - --- 2007-11-5 (November 5, 2007) ECHO DATEFMT yy mm dd - /LZ --- 2007-11-05 (November 5, 2007) ECHO DATEFMT mm /LZ --- 01 (January) ECHO DATEFMT yy mm - /LZ --- 2007-06 (June 2007) ECHO DATEFMT dd mm dd * /LZ --- 11*03*11 (March 11) ECHO. ECHO Inspired by Simon Sheppard's GetDate.bat ECHO http://ss64.com/ntsyntax/getdate.html ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com
This batch file returns part of the current date or time on screen.
It is a (partial) emulation of VBScript's DatePart( )
function.
@ECHO OFF ECHO. :: Check Windows version (XP Pro or later) IF NOT "%OS%"=="Windows_NT" GOTO Syntax :: Check command line argument (one mandatory) IF "%~1"=="" GOTO Syntax IF NOT "%~2"=="" GOTO Syntax :: Check if help is required ECHO.%1 | FINDSTR.EXE /R /C:"[/?\.]" >NUL && GOTO Syntax :: Check if WMIC is available WMIC.EXE Alias /? >NUL 2>&1 || GOTO Syntax :: Localize variables SETLOCAL ENABLEDELAYEDEXPANSION :: Fill array with valid arguments SET DatePart.d=Day SET DatePart.Day=Day SET DatePart.DayOfWeek=DayOfWeek SET DatePart.w=DayOfWeek SET DatePart.h=Hour SET DatePart.Hour=Hour SET DatePart.n=Minute SET DatePart.Minute=Minute SET DatePart.m=Month SET DatePart.Month=Month SET DatePart.q=Quarter SET DatePart.Quarter=Quarter SET DatePart.s=Second SET DatePart.Second=Second SET DatePart.yyyy=Year SET DatePart.Year=Year :: Check if command line argument is listed in array SET DatePart. | FINDSTR /R /I /C:"\.%~1=" >NUL IF ERRORLEVEL 1 ( ENDLOCAL GOTO Syntax ) :: Initialize variable SET Error=0 :: Use WMIC to display the requested part of the date or time FOR /F "skip=1" %%A IN ('WMIC Path Win32_LocalTime Get !DatePart.%~1! /Format:table 2^>NUL ^|^| SET Error=1') DO SET DatePart=%%A ECHO.%DatePart% :: Check for errors trapped by WMIC IF "%Error%"=="1" ( ENDLOCAL GOTO Syntax ) :: Done ENDLOCAL & SET DatePart=%DatePart% GOTO:EOF :Syntax ECHO DatePart.bat, Version 2.01 for Windows XP Pro and later ECHO Returns the specified part of the current date or time ECHO. ECHO Usage: DATEPART option ECHO. ECHO Where: option(s) display ECHO. ================= ============ ECHO d or Day day of month ECHO w or DayOfWeek day of week ECHO h or Hour hour ECHO n or Minute minutes ECHO m or Month month ECHO q or Quarter quarter ECHO s or Second seconds ECHO yyyy or Year year ECHO. ECHO Notes: All values returned are numeric, without leading zeros. ECHO The requested value is displayed on screen and stored ECHO in an environment variable %%DatePart%%. ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com
This batch file also uses the code from version 3 of SORTDATE.BAT to display the current week number:
@ECHO OFF :: Week.bat, Version 1.00 for Windows NT :: Display the number of the current week :: :: Written by Rob van der Woude :: http://www.robvanderwoude.com :: Use local copy of environment and enable command extensions VERIFY OTHER 2>nul SETLOCAL ENABLEEXTENSIONS IF ERRORLEVEL 1 ( ECHO Unable to enable command extensions GOTO End ) :: Export registry settings for date format to a temporary file START /W REGEDIT /E %TEMP%.\_TEMP.REG "HKEY_CURRENT_USER\Control Panel\International" :: Read date format from the exported data FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "iDate"') DO SET iDate=%%B FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "sDate"') DO SET sDate=%%B DEL %TEMP%.\_TEMP.REG :: Remove quotes SET iDate=%iDate:"=% SET sDate=%sDate:"=% :: Convert current date to number of days passed this year, :: taking into account the date format read from the registry IF %iDate%==0 FOR /F "tokens=2-4* delims=%sDate% " %%A IN ('DATE/T') DO CALL :DoY %%B %%A %%C IF %iDate%==1 FOR /F "tokens=2-4* delims=%sDate% " %%A IN ('DATE/T') DO CALL :DoY %%A %%B %%C IF %iDate%==2 FOR /F "tokens=2-4* delims=%sDate% " %%A IN ('DATE/T') DO CALL :DoY %%C %%B %%A :: Convert day of week to number FOR /F "tokens=1 delims= " %%A IN ('DATE/T') DO CALL :DoW %%A :: Calculate number of full weeks passed this year SET /A Week = %DoY% - %DoW% + 7 SET /A Week /= 7 SET Week GOTO End :DoW SETLOCAL :: Convert day of week to number (language dependent, modify if necessary!) IF "%1"=="Mon" SET DoW=1 IF "%1"=="Tue" SET DoW=2 IF "%1"=="Wed" SET DoW=3 IF "%1"=="Thu" SET DoW=4 IF "%1"=="Fri" SET DoW=5 IF "%1"=="Sat" SET DoW=6 IF "%1"=="Sun" SET DoW=7 ENDLOCAL & SET DoW=%DoW% GOTO:EOF :DoY SETLOCAL SET DoM=%1 :: Remove leading zero IF "%DoM:~0,1%"=="0" IF %DoM% GTR 0 SET /A DoM = 1%DoM% - 100 SET Month=%2 :: Remove leading zero IF "%Month:~0,1%"=="0" IF %Month% GTR 0 SET /A Month = 1%Month% - 100 SET Year=%3 :: Determine leap day SET LeapYear=0 SET /A Leap = %Year% %% 4 IF %Leap% EQU 0 SET LeapYear=1 SET /A Leap = %Year% %% 100 IF %Leap% EQU 0 SET LeapYear=0 SET /A Leap = %Year% %% 400 IF %Leap% EQU 0 SET LeapYear=1 :: Add the days of each full month passed IF %Month% GEQ 1 SET /A Days2Add = 0 IF %Month% GEQ 2 SET /A Days2Add += 31 IF %Month% GEQ 3 SET /A Days2Add += 28 IF %Month% GEQ 3 SET /A Days2Add += %LeapYear% IF %Month% GEQ 4 SET /A Days2Add += 31 IF %Month% GEQ 5 SET /A Days2Add += 30 IF %Month% GEQ 6 SET /A Days2Add += 31 IF %Month% GEQ 7 SET /A Days2Add += 30 IF %Month% GEQ 8 SET /A Days2Add += 31 IF %Month% GEQ 9 SET /A Days2Add += 31 IF %Month% GEQ 0 SET /A Days2Add += 30 IF %Month% GEQ 11 SET /A Days2Add += 31 IF %Month% GEQ 12 SET /A Days2Add += 30 :: Add it all up and return the calculated value SET /A DoY = %Days2Add% + %DoM% ENDLOCAL & SET DoY=%DoY% GOTO:EOF :End ENDLOCAL
This batch file uses that same code snippet from version 3 of SORTDATE.BAT too, to display yesterday's, today's and tomorrow's dates in two formats:
@ECHO OFF :: Windows NT 4 or later only IF NOT "%OS%"=="Windows_NT" GOTO Syntax :: No command line arguments required IF NOT [%1]==[] GOTO Syntax :: Keep variables local SETLOCAL ENABLEDELAYEDEXPANSION :: Query the registry for the date format and delimiter CALL :DateFormat :: Parse today's date depending on registry's local date format settings IF %iDate%==0 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO ( SET LocalFormat=MM%sDate%DD%sDate%YYYY SET tLocal=%%tMonth%%%sDate%%%tDay%%%sDate%%%tYear%% SET yLocal=%%yMonth%%%sDate%%%yDay%%%sDate%%%yYear%% SET Year=%%C SET Month=%%A SET Day=%%B ) IF %iDate%==1 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO ( SET LocalFormat=DD%sDate%MM%sDate%YYYY SET tLocal=%%tDay%%%sDate%%%tMonth%%%sDate%%%tYear%% SET yLocal=%%yDay%%%sDate%%%yMonth%%%sDate%%%yYear%% SET Year=%%C SET Month=%%B SET Day=%%A ) IF %iDate%==2 FOR /F "TOKENS=1-4* DELIMS=%sDate%" %%A IN ('DATE/T') DO ( SET LocalFormat=YYYY%sDate%MM%sDate%DD SET tLocal=%%tYear%%%sDate%%%tMonth%%%sDate%%%tDay%% SET yLocal=%%yYear%%%sDate%%%yMonth%%%sDate%%%yDay%% SET Year=%%A SET Month=%%B SET Day=%%C ) :: Remove the day of week if applicable FOR %%A IN (%Year%) DO SET Year=%%A FOR %%A IN (%Month%) DO SET Month=%%A FOR %%A IN (%Day%) DO SET Day=%%A :: Today's date in YYYYMMDD format SET SortDate=%Year%%Month%%Day% :: Today's date in local format FOR %%A IN (%Date%) DO SET Today=%%A :: Calculate yesterday's date CALL :JDate %Year% %Month% %Day% SET /A JDate -= 1 CALL :GDate %JDate% FOR /F "tokens=1-3" %%A IN ('ECHO %GDate%') DO ( SET yYear=%%A SET yMonth=%%B SET yDay=%%C ) :: Calculate tomorrow's date SET /A JDate += 2 CALL :GDate %JDate% FOR /F "tokens=1-3" %%A IN ('ECHO %GDate%') DO ( SET tYear=%%A SET tMonth=%%B SET tDay=%%C ) :: Add leading zero to tDay, yDay, tMonth and/or yMonth if necessary IF 1%tDay% LSS 20 SET tDay=0%tDay% IF 1%yDay% LSS 20 SET yDay=0%yDay% IF 1%tMonth% LSS 20 SET tMonth=0%tMonth% IF 1%yMonth% LSS 20 SET yMonth=0%yMonth% :: Yesterday's and tomorrow's date in YYYYMMDD format SET SortTom=%tYear%%tMonth%%tDay% SET SortYest=%yYear%%yMonth%%yDay% :: Display the results ECHO Format: YYYYMMDD (%LocalFormat%) ECHO.================================== CALL ECHO Yesterday: %SortYest% (%yLocal%) ECHO Today: %SortDate% (%Today%) CALL ECHO Tomorrow: %SortTom% (%tLocal%) :: Done ENDLOCAL&SET Yesterday=%SortYest%&SET Today=%SortDate%&SET Tomorrow=%SortTom% GOTO:EOF :: * * * * * * * * Subroutines * * * * * * * * :DateFormat REG.EXE /? 2>&1 | FIND "REG QUERY" >NUL IF ERRORLEVEL 1 ( CALL :DateFormatRegEdit ) ELSE ( CALL :DateFormatReg ) GOTO:EOF :DateFormatReg FOR /F "tokens=1-3" %%A IN ('REG Query "HKCU\Control Panel\International" ^| FINDSTR /R /C:"[is]Date"') DO ( IF "%%~A"=="REG_SZ" ( SET %%~B=%%~C ) ELSE ( SET %%~A=%%~C ) ) GOTO:EOF :DateFormatRegEdit :: Export registry's date format settings to a temporary file, in case REG.EXE is not available START /W REGEDIT /E %TEMP%.\_TEMP.REG "HKEY_CURRENT_USER\Control Panel\International" :: Read the exported data FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "iDate"') DO SET iDate=%%B FOR /F "tokens=1* delims==" %%A IN ('TYPE %TEMP%.\_TEMP.REG ^| FIND /I "sDate"') DO SET sDate=%%B DEL %TEMP%.\_TEMP.REG :: Remove quotes from exported values SET iDate=%iDate:"=% SET sDate=%sDate:"=% GOTO:EOF :GDate :: Convert Julian date back to "normal" Gregorian date :: Argument : Julian date :: Returns : YYYY MM DD :: :: Algorithm based on Fliegel-Van Flandern :: algorithm from the Astronomical Almanac, :: provided by Doctor Fenton on the Math Forum :: (http://mathforum.org/library/drmath/view/51907.html), :: and converted to batch code by Ron Bakowski. :: SET /A P = %1 + 68569 SET /A Q = 4 * %P% / 146097 SET /A R = %P% - ( 146097 * %Q% +3 ) / 4 SET /A S = 4000 * ( %R% + 1 ) / 1461001 SET /A T = %R% - 1461 * %S% / 4 + 31 SET /A U = 80 * %T% / 2447 SET /A V = %U% / 11 SET /A GYear = 100 * ( %Q% - 49 ) + %S% + %V% SET /A GMonth = %U% + 2 - 12 * %V% SET /A GDay = %T% - 2447 * %U% / 80 :: Clean up the mess FOR %%A IN (P Q R S T U V) DO SET %%A= :: Add leading zerîs IF 1%GMonth% LSS 20 SET GMonth=0%GMonth% IF 1%GDay% LSS 20 SET GDay=0%GDay% :: Return value SET GDate=%GYear% %GMonth% %GDay% GOTO:EOF :JDate :: Convert date to Julian :: Arguments : YYYY MM DD :: Returns : Julian date :: :: Algorithm based on Fliegel-Van Flandern :: algorithm from the Astronomical Almanac, :: provided by Doctor Fenton on the Math Forum :: (http://mathforum.org/library/drmath/view/51907.html), :: and converted to batch code by Ron Bakowski. :: SET /A Month1 = ( %2 - 14 ) / 12 SET /A Year1 = %1 + 4800 SET /A JDate = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %2 - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %3 - 32075 FOR %%A IN (Month1 Year1) DO SET %%A= GOTO:EOF :UpCase SET UpCase=%1 SET UpCase=%UpCase:a=A% SET UpCase=%UpCase:b=B% SET UpCase=%UpCase:c=C% SET UpCase=%UpCase:d=D% SET UpCase=%UpCase:e=E% SET UpCase=%UpCase:f=F% SET UpCase=%UpCase:g=G% SET UpCase=%UpCase:h=H% SET UpCase=%UpCase:i=I% SET UpCase=%UpCase:j=J% SET UpCase=%UpCase:k=K% SET UpCase=%UpCase:l=L% SET UpCase=%UpCase:m=M% SET UpCase=%UpCase:n=N% SET UpCase=%UpCase:o=O% SET UpCase=%UpCase:p=P% SET UpCase=%UpCase:q=Q% SET UpCase=%UpCase:r=R% SET UpCase=%UpCase:s=S% SET UpCase=%UpCase:t=T% SET UpCase=%UpCase:u=U% SET UpCase=%UpCase:v=V% SET UpCase=%UpCase:w=W% SET UpCase=%UpCase:x=X% SET UpCase=%UpCase:y=Y% SET UpCase=%UpCase:z=Z% GOTO:EOF :Syntax ECHO. ECHO Yesterday.bat, Version 3.00 for Windows NT ECHO Display yesterday's, today's and tomorrow's dates in sorted and local format ECHO. IF "%OS%"=="Windows_NT" CALL :UpCase %~n0 IF "%OS%"=="Windows_NT" ECHO Usage: %UpCase% IF NOT "%OS%"=="Windows_NT" ECHO Usage: %0 ECHO. ECHO Notes: Environment variables Yesterday, Today and Tomorrow are set to the ECHO sorted date values found. ECHO Adapted for Windows XP with help from Kailash Chanduka. ECHO Local date code by Frederic Guigand and Rob van der Woude. ECHO Julian date math algorithms based on Fliegel-Van Flandern algorithm ECHO from the Astronomical Almanac, provided by Doctor Fenton on the Math ECHO Forum (http://mathforum.org/library/drmath/view/51907.html), and ECHO converted to batch code by Ron Bakowski. ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com IF "%OS%"=="Windows_NT" COLOR 00
This batch file calculates the dates for Easter Day, Ascension Day and Pentecost in any year.
It also uses that same code snippet from version 3 of SORTDATE.BAT, to determine the current year.
The algorithm used to calculate the Easter date was found at Simon Kershaw's "KEEPING THE FEAST, A companion to the Holy Days of the Calendar 2000".
@ECHO OFF ECHO. :: Windows NT 4 / 2000 / XP only IF NOT "%OS%"=="Windows_NT" GOTO Syntax :: Localize environment and enable delayed variable expansion SETLOCAL ENABLEDELAYEDEXPANSION :: Command line check IF NOT [%2]==[] GOTO Syntax :: Determine current year CALL :ThisYear :: If no year is specified, use current year IF "%~1"=="" (SET Y=%ThisYear%) ELSE (SET Y=%~1) :: Is the specified year valid? :: Check if number FOR /F "tokens=1 delims=0123456789" %%A IN ('ECHO.%Y%') DO IF NOT "%%~A"=="" GOTO Syntax :: check if in range IF %Y%0 LSS 17520 GOTO Syntax IF %Y%0 GTR 30000 GOTO Syntax :: Array of month names used SET Month.03=March SET Month.3=March SET Month.04=April SET Month.4=April SET Month.05=May SET Month.5=May SET Month.06=June SET Month.6=June :: Calculate Easter Day using the instructions found at :: Simon Kershaw's "KEEPING THE FEAST" :: http://www.oremus.org/liturgy/etc/ktf/app/easter.html SET /A G = ( %Y% %% 19 ) + 1 SET /A S = (( %Y% - 1600 ) / 100 ) - (( %Y% - 1600 ) / 400 ) SET /A L = ((( %Y% - 1400 ) / 100 ) * 8 ) / 25 SET /A P1 = ( 30003 - 11 * %G% + %S% - %L% ) %% 30 SET P=%P1% IF %P%==28 IF %G% GTR 11 SET P=27 IF %P%==29 SET P=28 SET /A D = ( %Y% + ( %Y% / 4 ) - ( %Y% / 100 ) + ( %Y% / 400 )) %% 7 SET /A D1 = ( 8 - %D% ) %% 7 SET /A P2 = ( 70003 + %P% ) %% 7 SET /A X = (( 70004 - %D% - %P% ) %% 7 ) + 1 SET /A E = %P% + %X% IF %E% LSS 11 ( SET /A ED = %E% + 21 SET EM=3 ) ELSE ( SET /A ED = %E% - 10 SET EM=4 ) IF %Y% LSS %ThisYear% SET IS=was IF %Y% EQU %ThisYear% SET IS=is IF %Y% GTR %ThisYear% SET IS=will be :: Calculate Ascension and Pentecost dates CALL :JDate %Y% %EM% %ED% SET /A ADJ = %JDate% + 39 SET /A PDJ = %JDate% + 49 CALL :GDate %ADJ% FOR /F "tokens=2,3" %%A IN ("%GDate%") DO ( SET AM=%%A SET AD=%%B ) CALL :GDate %PDJ% FOR /F "tokens=2,3" %%A IN ("%GDate%") DO ( SET PM=%%A SET PD=%%B ) :: Display the result ECHO In %Y% Easter Day %IS% !Month.%EM%! %ED% ECHO Ascension Day %IS% !Month.%AM%! %AD% ECHO Pentecost %IS% !Month.%PM%! %PD% :: Done GOTO End :GDate :: Convert Julian date back to "normal" Gregorian date :: Argument : Julian date :: Returns : YYYY MM DD :: :: Algorithm based on Fliegel-Van Flandern :: algorithm from the Astronomical Almanac, :: provided by Doctor Fenton on the Math Forum :: (http://mathforum.org/library/drmath/view/51907.html), :: and converted to batch code by Ron Bakowski. :: SET /A P = %1 + 68569 SET /A Q = 4 * %P% / 146097 SET /A R = %P% - ( 146097 * %Q% +3 ) / 4 SET /A S = 4000 * ( %R% + 1 ) / 1461001 SET /A T = %R% - 1461 * %S% / 4 + 31 SET /A U = 80 * %T% / 2447 SET /A V = %U% / 11 SET /A GYear = 100 * ( %Q% - 49 ) + %S% + %V% SET /A GMonth = %U% + 2 - 12 * %V% SET /A GDay = %T% - 2447 * %U% / 80 :: Clean up the mess FOR %%A IN (P Q R S T U V) DO SET %%A= :: Add leading zerîs IF 1%GMonth% LSS 20 SET GMonth=0%GMonth% IF 1%GDay% LSS 20 SET GDay=0%GDay% :: Return value SET GDate=%GYear% %GMonth% %GDay% GOTO:EOF :JDate :: Convert date to Julian :: Arguments : YYYY MM DD :: Returns : Julian date :: :: Algorithm based on Fliegel-Van Flandern :: algorithm from the Astronomical Almanac, :: provided by Doctor Fenton on the Math Forum :: (http://mathforum.org/library/drmath/view/51907.html), :: and converted to batch code by Ron Bakowski. :: SET /A Month1 = ( %2 - 14 ) / 12 SET /A Year1 = %1 + 4800 SET /A JDate = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %2 - 2 -12 * %Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %3 - 32075 FOR %%A IN (Month1 Year1) DO SET %%A= GOTO:EOF :ThisYear :: Export registry settings to a temporary file START /WAIT REGEDIT /E "%Temp%.\_Temp.reg" "HKEY_CURRENT_USER\Control Panel\International" :: Read iDate and sDate from the exported data; more info on iDate can be found at :: http://technet2.microsoft.com/windowsserver/en/library/7dedbd31-40bd-4f47-a833-517a0b9ab9bb1033.mspx :: and more info on sDate at :: http://technet2.microsoft.com/windowsserver/en/library/072ad962-21c4-4070-9c6f-2720922d6d361033.mspx FOR /F "tokens=1* delims==" %%A IN ('TYPE "%Temp%.\_Temp.reg" ^| FINDSTR /R /B /C:"\"[is]Date\"="') DO SET %%~A=%%~B DEL "%Temp%.\_Temp.reg" :: Detemine current year depending on registry settings FOR %%A IN (%Date%) DO SET Today=%%A IF %iDate%==2 (SET Token=1) ELSE (SET Token=3) FOR /F "tokens=%Token% delims=%sDate%" %%A IN ("%Today%") DO SET ThisYear=%%A GOTO:EOF :Syntax ECHO Easter.bat, Version 3.00 for Windows NT 4 and later ECHO Calculate Easter day, Ascension day and Pentecost dates for the specified year. ECHO. ECHO Usage: EASTER [ year ] ECHO. ECHO Where: year should be within the range of 1752..3000 (default: current year) ECHO. ECHO Note: Easter day calculation based on Simon Kershaw's "KEEPING THE FEAST" ECHO (http://www.oremus.org/liturgy/etc/ktf/app/easter.html). ECHO Julian date math algorithms based on Fliegel-Van Flandern algorithm ECHO from the Astronomical Almanac, provided by Doctor Fenton on the Math ECHO Forum (http://mathforum.org/library/drmath/view/51907.html), and ECHO converted to batch code by Ron Bakowski. ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com :End IF "%OS%"=="Windows_NT" ENDLOCAL
This batch file checks if the specified or current year is a leap year.
It uses a code snippet from Simon Sheppard's GetDate.bat:
@ECHO OFF :: Windows NT 4 and later only IF NOT "%OS%"=="Windows_NT" GOTO Syntax SETLOCAL :: Use current year as default IF [%1]==[] GOTO ThisYear :: Only 1 parameter allowed IF NOT [%2]==[] GOTO Syntax :: Check for ? ECHO.%1 | FIND "?" >NUL IF NOT ERRORLEVEL 1 GOTO Syntax :: Check if parameter is a number IF 1%1 LSS 2 GOTO Syntax :: Check if the number is within range IF 1%1 LSS 10 GOTO Syntax IF 1%1 GTR 19999 GOTO Syntax :: OK, continue SET yy=%1 GOTO Calculate :ThisYear :: Code to extract day, month and year by Simon Sheppard :: http://www.ss64.com FOR /f "tokens=2-4 skip=1 delims=(/-)" %%G IN ('ECHO.^|DATE') DO ( FOR /f "tokens=2 delims= " %%A IN ('DATE /T') DO ( SET v_first=%%G SET v_second=%%H SET v_third=%%I SET v_all=%%A ) ) SET %v_first%=%v_all:~0,2% SET %v_second%=%v_all:~3,2% SET %v_third%=%v_all:~6,4% :Calculate :: Default to not a leap year SET LeapYear=0 :: If year is a multiple of 4 then it is a leap year ... SET /A test = %yy% / 4 SET /A test = %test% * 4 IF %test% EQU %yy% SET LeapYear=1 :: ... except if it is a multiple of 100 ... SET /A test = %yy% / 100 SET /A test = %test% * 100 IF %test% EQU %yy% SET LeapYear=0 :: ... unless it is a multiple of 400! SET /A test = %yy% / 400 SET /A test = %test% * 400 IF %test% EQU %yy% SET LeapYear=1 :: Display the result SET is=IS IF %LeapYear%==0 SET is=is NOT ECHO %yy% %is% a leap year :: Done ENDLOCAL GOTO End :Syntax ECHO. ECHO LeapYear.bat, Version 1.00 for Windows NT 4 / 2000 / XP ECHO Check if the specified year is a leap year or not. ECHO. ECHO Usage: LEAPYEAR [ year ] ECHO Where: "year" is a year between 0 and 9999. ECHO If no year is specified, the current year is assumed. ECHO. ECHO Written by Rob van der Woude ECHO http://www.robvanderwoude.com ECHO. ECHO Code to extract the current year written by Simon Sheppard ECHO http://www.ss64.com :End
page last modified: 2016-09-19; loaded in 0.0022 seconds