GetDate.cmd
@echo off&SETLOCAL :: This will return date into environment vars :: Works on any NT/2K/XP machine independent of regional date settings :: 20 March 2002 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 echo Month:[%mm%] Day:[%dd%] Year:[%yy%] ENDLOCAL&SET mm=%mm%&SET dd=%dd%&SET yy=%yy%
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.
Related
alt.msdos.batch.nt - thread on this topic
Enhancement by Gary Deane
Commandline.co.uk - date/time scripts
Rob Vanderwoude - date/time scripts