@echo off&SETLOCAL
:: This will return the date into environment variables
:: 2002-03-20 : Works on any NT/2K/XP machine independent of regional date settings
:: 2011-05-04 : Updated to also handle the German language under Windows 7
FOR /f "tokens=1-4 delims=/-. " %%G IN ('date /t') DO (call :s_fixdate %%G %%H %%I %%J)
goto :s_print_the_date
:s_fixdate
if "%1:~0,1%" GTR "9" shift
FOR /f "skip=1 tokens=2-4 delims=(-)" %%G IN ('echo.^|date') DO (
Set %%G=%1&set %%H=%2&Set %%I=%3)
goto :eof
:s_print_the_date
Endlocal&(
Echo.|date|find "JJ">nul
If errorlevel 1 (
:: English locale
Echo Year:[%yy%] Month:[%mm%] Day:[%dd%]
SET yy=%yy%&SET mm=%mm%&SET dd=%dd%
) Else (
:: German locale
Echo Jahr:[%JJ%] Monat:[%MM%] Tag:[%TT%]
SET yy=%JJ%&Set mm=%MM%&SET dd=%TT%
))
The above works by taking advantage of a quirk in the date command first noticed by Michael Jerkovic - the DATE command will display something like:
The current date is: Fri 14/04/2000
Enter the new date: (dd-mm-yy)
The useful bit there is the (dd-mm-yy), on a machine with different regional settings it may appear as (mm-dd-yy)
The first FOR command returns the 3 numeric parts of the date e.g. 14 04 2000
The second FOR command returns the 3 text descriptions e.g. dd mm yy
Then the line set %%G=%1&set %%H=%2&set %%I=%3 creates the 3 variables and sets them = numeric values
Effectively: set dd=14&set mm=04&set yy=2000
On a machine with different regional settings this might become: set mm=04&set dd=14&set yy=2000
Note the reason this works with any regional setting is that the DATE string (dd-mm-yy) always displays with `-` as it's date separator.
"I've been on the calendar, but never on time" - Marilyn Monroe
Related
datetime.vbs - Get Date, Time and daylight savings (VB Script)
alt.msdos.batch.nt - thread on this topic
Enhancement by Gary Deane
Commandline.co.uk - date/time scripts
Rob Vanderwoude - date/time scripts