Split %date% in a batch file regardless of Regional Settings

17,231

Solution 1

You can do it using wmic (but WMIC isn't included with XP Home):

@ECHO OFF
:: Check WMIC is available
WMIC.EXE Alias /? >NUL 2>&1 || GOTO s_error

:: Use WMIC to retrieve date and time
FOR /F "skip=1 tokens=1-6" %%G IN ('WMIC Path Win32_LocalTime Get Day^,Hour^,Minute^,Month^,Second^,Year /Format:table') DO (
   IF "%%~L"=="" goto s_done
      Set _yyyy=%%L
      Set _mm=00%%J
      Set _dd=00%%G
      Set _hour=00%%H
      SET _minute=00%%I
)
:s_done

:: Pad digits with leading zeros
      Set _mm=%_mm:~-2%
      Set _dd=%_dd:~-2%
      Set _hour=%_hour:~-2%
      Set _minute=%_minute:~-2%

:: Display the date/time in ISO 8601 format:
Set _isodate=%_yyyy%-%_mm%-%_dd% %_hour%:%_minute%
Echo %_isodate%

GOTO:EOF

:s_error
Echo GetDate.cmd
Echo Displays date and time independent of OS Locale, Language or date format.
Echo Requires Windows XP Professional, Vista or Windows 7
Echo.
Echo Returns 6 environment variables containing isodate,Year,Month,Day,hour and minute.

And you can do it by parsing the date command to lookup what the current date format is required to be.

The first link indicates you might need to edit the code in the second, on Win7, to handle a few extra wrinkles around short date/long date form.

Solution 2

Four years have passed, but this question doesn't get old, and I think now there's a slightly better answer than using wmic (on win7 onwards).

for /F "tokens=1,2,3 delims=_" %%i in ('PowerShell -Command "& {Get-Date -format "MM_dd_yyyy"}"') do (
    set MONTH=%%i
    set DAY=%%j
    set YEAR=%%k
)
echo %MONTH% %DAY% %YEAR%

With powershell Get-Date you can fine-tune the format you want (short,long, numbers,names,etc..). In this example "MM_dd_yyyy" will get you a numeric date with leading zeros in case of single digit months or days

Solution 3

I've reworked sashoalm's version to take care of the suppressed-leading-zero situation:

@Echo OFF
SETLOCAL
If "%Date%A" LSS "A" (Set _NumTok=1-3) Else (Set _NumTok=2-4)
:: Default Delimiter of TAB and Space are used
For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v iDate') Do Set _iDate=%%B
For /F "TOKENS=2*" %%A In ('REG QUERY "HKCU\Control Panel\International" /v sDate') Do Set _sDate=%%B
IF %_iDate%==0 For /F "TOKENS=%_NumTok% DELIMS=%_sdate% " %%F In ("%Date%") Do CALL :procdate %%H %%F %%G
IF %_iDate%==1 For /F "TOKENS=%_NumTok% DELIMS=%_sdate% " %%F In ("%Date%") Do CALL :procdate %%H %%G %%F
IF %_iDate%==2 For /F "TOKENS=%_NumTok% DELIMS=%_sdate% " %%F In ("%Date%") Do CALL :procdate %%F %%G %%H
endlocal&SET YYYYMMDD=%YYYYMMDD%
GOTO :eof

::
:: Date elements are supplied in Y,M,D order but may have a leading zero
::
:procdate
:: if single-digit day then 1%3 will be <100 else 2-digit
IF 1%3 LSS 100 (SET YYYYMMDD=0%3) ELSE (SET YYYYMMDD=%3)
:: if single-digit month then 1%2 will be <100 else 2-digit
IF 1%2 LSS 100 (SET YYYYMMDD=0%2%YYYYMMDD%) ELSE (SET YYYYMMDD=%2%YYYYMMDD%)
:: Similarly for the year - I've never seen a single-digit year
IF 1%1 LSS 100 (SET YYYYMMDD=20%YYYYMMDD%) ELSE (SET YYYYMMDD=%1%YYYYMMDD%)
GOTO :eof

returning YYYYMMDD - substring at your will.

Interestingly, inserting after SETLOCAL

IF NOT "%1"=="" set date=%1

will allow any date in the local sequence (without the dayname) to be decoded to YYYYMMDD (but be careful that 19xx dates provided with the yy form will appear as 20xx - easily compensated-for if you find it necessary)

Solution 4

Try this: Set_date.cm

@echo off&goto :start
:Set_Date DateEnv [Date] or [/p="Prompt for Date: "] [/r]
echo.
echo.Usage: Set_Date.cmd DateEnv [Date] or [/p="Prompt for Date: "]
echo.Populates date variables labeled: 'DateEnv_???', see below
echo.Accepted date formats:
echo.        m-d-y, m/d/y, yyyymmdd, dd-Mon-yy or 7 digit julian date
echo.        If "%%date%%" is passed as the 2nd arg it will be interpreted
echo.        according to the current regional date setting ie. day dd/mm/yyyy
echo.        If /r is passed then the date contained in the DateEnv variable
echo.        and/or the [Date] argument will be interpreted in regional format
echo.        The /r option should be placed at the end of the list if used
echo.        The /r option should only be used if date input is not included
echo.        in one of the default listed formats or unexpected consequences...
echo.
echo.DateEnv, if passed alone may be a valid defined date variable or if
echo.undefined then todays date will be used to populate the variables
echo.Two digit years input 'XX' are interpreted as the year '20XX'
echo.Valid dates must be in the year 1601 or greater for the validity check
echo.Returns 0 in errorlevel if the date is valid, else non-zero
echo.
echo.If DateEnv is valid or if [Date] is passed DateEnv_??? will be set to:
echo.DateEnv_mdy will be set to mm-dd-yyyy
echo.DateEnv_ymd will be set to yyyymmdd
echo.DateEnv_y.m.d will be set to yyyy.mm.dd
echo.DateEnv_dte will be set to the current active regional date format
echo.DateEnv_jul will be set to the 7 digit Julian date
echo.DateEnv_dow will be set to day of week, ie. Monday, Tuesday, etc.
echo.DateEnv_vrb will be set to Verbose Date, ie. Saturday, August 20th, 2011
echo.DateEnv_dd will be set to the 2 digit day
echo.DateEnv_mm will be set to the 2 digit month
echo.DateEnv_yyyy will be set to the 4 digit year
echo.DateEnv_month will be set to the month, ie. February or December
echo.DateEnv_mon will be set to the 3 letter month, ie. Jan or Aug
echo.
echo.DateEnv itself will not be changed regardless if its a valid date or not
echo.Also formats yyyymmdd or 7 digit julian date input to desired output
echo.Example: Set_Date today "%%date%%" or Set_Date newyear99 01-Jan-99
echo.
echo.Finally:
echo.If DateEnv is not defined and there is no second argument then %%date%% in
echo.the current regional format will be used to populate the variables
echo.If /p is passed as 2nd argument then quoted 3rd argument will prompt for date
echo.
echo.Examples:
echo.         set "AprilsFools=4/1/1"
echo.         set_date AprilFools
echo.         echo Aprils fools date for 2001 in Julian format is: %%AprilFools_jul%%
echo.         echo Aprils fools date in 2001 was on a %AprilFools_dow% ^(Sunday^)
echo.         Set_Date birthday /p="Enter your birth date: "
echo.         ECHO You were born on a %%birthday_dow%%
echo.         set "today="
echo.         Set_Date today
echo.         echo today is %%today_dte%% or %%today_jul%% in Julian format
echo.
echo.If the '/p' option is used then the _GetString.cmd routine must be in the PATH
echo.         or else set/p routine will ask for date with no validity checking
exit /b 1

:start
set "jul="&set "Format="&set "Sep="&set "reg="
if "%~1"=="" exit /b 255
if /i "%~1" equ "/?" goto :Set_Date Syntax
SetLocal EnableDelayedExpansion
echo %*|find /i "/r">nul
if %errorlevel% equ 0 set/a reg=1&if /i "%~1" equ "/r" shift
if defined %~1 (call set jul=%%%~1%%) else (
  if "%~2"=="" set "jul=%date%"
)
if not "%~2"=="" (
  if /i "%~2" neq "/r" (
    set "jul=%~2"&set "args=%~3"
  )
) else (if not defined %~1 set/a reg=1)
call :RegDateFmt Format Separator yy mm dd jul
if %errorlevel% neq 0 goto :BadDate
if /i "%~2" equ "%date%" set/a reg=1
if defined reg (
  set "jul=!mm!-!dd!-!yy!"
)
if /i "%jul%" equ "/p" (
  call :_GetString.cmd "%args%" jul /d
  if !errorlevel! gtr 0 goto :BadDate
) else if /i "%jul:~0,1%" gtr "9" (
  if defined args set "jul=%jul% %args%"
  set "jul=!jul:* =!"
) else if /i "%jul:~3,1%" gtr "9" (
  set "Mon=%jul:~3,3%"
  call :month_convert Mon
  if !errorlevel! gtr 0 goto :BadDate
  set "jul=!Mon!-%jul:~0,2%-%jul:~-2%"
)
set mdy=%jul:/=%
set mdy=%mdy:-=%
if /i %mdy% equ %jul% (
  call :strlen %mdy%
  if /i !errorlevel! equ 7 ( 
    call :date_cvt mdy /j
  ) else (call :date_cvt jul /j)
) else (call :date_cvt jul /j)
if /i %errorlevel% equ 0 (
  set/a mdy=%jul%
  set/a dow=%jul% %% 7&call :set_day dow
) else (goto :BadDate)
call :date_cvt mdy /j
set "vrb=%mdy%"
call :format_verbose vrb dow month
set/a ymd=%errorlevel%
set "mon=%month:~0,3%"
set/a dte=%ymd%
call :setRegDate dte Format Separator dow mon
if /i %errorlevel% gtr 0 goto :BadDate
Endlocal&(
  call set "%~1_mdy=%mdy%"&call set "%~1_ymd=%ymd%"&call set "%~1_jul=%jul%"
  call set "%~1_vrb=%vrb%"&call set "%~1_dow=%dow%"&call set "%~1_dd=%ymd:~-2%"
  call set "%~1_mm=%mdy:~0,2%"&call set "%~1_yyyy=%ymd:~0,4%"
  call set "%~1_mon=%mon%"&call set "%~1_yy=%ymd:~2,2%"&call set "%~1_dte=%dte%"
  call set "%~1_y.m.d=%ymd:~0,4%.%mdy:~0,2%.%ymd:~-2%"&call set "%~1_month=%month%"
  exit /b 0
)
:BadDate
Endlocal&(
  call set "%~1_mdy="&call set "%~1_ymd="&call set "%~1_dte="&call set "%~1_jul="
  call set "%~1_vrb="&call set "%~1_dow="&call set "%~1_vrb="&call set "%~1_y.m.d="
  call set "%~1_month="
)&exit /b %errorlevel%

::**********************************::
::*******||  SUBROUTINES   ||*******::
::**********************************::

:set_day
SetLocal&call set/a tkn=%%%~1%%+1
for /f "tokens=%tkn%" %%a in ("Monday Tuesday Wednesday Thursday Friday Saturday Sunday") do (
  set "dayofwk=%%a"
)
EndLocal&call set %~1=%dayofwk%&exit /b 1

:Date_Cvt DateEnv [/Julian] Date_env is converted
if "%~1"=="" exit /b 1
SetLocal&call set "mdy=%%%~1%%"
set ech=&set "arg="
if not "%~2"=="" (set arg=%~2&set arg=!arg:~0,2!)
xcopy /d:%mdy% /h /l "%~f0" "%~f0\">nul 2>&1
if /i %errorlevel% equ 0 (
  for /f "tokens=1-3 delims=/- " %%a in ("%mdy%") do (
    set m=%%a&set d=%%b&set "y=%%c"
    if /i 1!m! lss 20 set "m=0!m!"
    if /i 1!d! lss 20 set "d=0!d!"
    if /i 1!y! lss 20 set "y=0!y!"
    if /i !y! lss 80 (set y=20!y!) else (if !y! lss 100 set y=19!y!)
    set "mdy=!m!-!d!-!y!"
  )
) else (
  set /a rc=1
  for /f "tokens=1-3 delims=0123456789" %%a in ("%mdy%") do set "rc=%%a"
  if /i !rc! neq 1 set /a err=2&goto :end
  call :strlen %mdy%
  if /i !errorlevel! gtr 8 set /a err=3&goto :end
)
set "mdy=%mdy:/=-%"
set "ymd=%mdy:-=%"
if %ymd%==%mdy% (
  call :strlen %ymd%
  set /a err=!errorlevel!
  if /i !err! equ 7 if /i "%arg%" equ "/j" (
    call :gdate %ymd%
    set /a ymd=!errorlevel!
    set /a err=8&set "arg="
  )
  if /i !err! neq 8 goto :end
  set mdy=!ymd:~4,2!-!ymd:~6,2!-!ymd:~0,4!&set "ech=!mdy!"
) else (
  set ymd=%ymd:~4,4%%ymd:~0,4%&set "ech=!ymd!"
)
xcopy /d:%mdy% /h /l "%~f0" "%~f0\">nul 2>&1
set /a err=%errorlevel%
if /i %err% neq 0 (set ech=)
if /i %err% equ 0 if /i "%arg%" equ "/j" (
  call :jdate %ymd%
  set /a ech=!errorlevel!
)
:end
EndLocal&call set "%~1=%ech%"&exit /b %err%

:Strlen Returns length of string in errorlevel
setlocal&set "#=%*"
if not defined # exit /b 0
set/a len=0
:loop
set/a len+=1
set "#=!#:~1!"&if not defined # endlocal&exit/b %len%
goto :loop

:jdate
SetLocal
set "yyyymmdd=%~1"
set "yyyy=%yyyymmdd:~0,4%"
set "mm=%yyyymmdd:~4,2%"
set "dd=%yyyymmdd:~6,2%"
if %mm:~0,1% equ 0 set "mm=%mm:~1%"
if %dd:~0,1% equ 0 set "dd=%dd:~1%"
set /a Month1=(%mm%-14)/12
set /a Year1=%yyyy%+4800
set /a JDate=1461*(%Year1%+%Month1%)/4+367*(%mm%-2-12*%Month1%)^
             /12-(3*((%Year1%+%Month1%+100)/100))/4+%dd%-32075
EndLocal&exit /b %JDate%

:gdate
SetLocal
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
if /i 1%GMonth% lss 20 set "GMonth=0%GMonth%"
if /i 1%GDay%   lss 20 set "GDay=0%GDay%"
set "GDate=%GYear%%GMonth%%GDay%"
EndLocal&exit /b %GDate%

:Format_Verbose M/D/Y dayofweek to verbose date
SetLocal&call set "dte=%%%~1%%"
set "dow=%%%~2%%"
set "st="
set "day=%dte:~3,2%"
set "mon=%dte:~0,2%"
set "year=%dte:~6,4%"
set "ymd=%year%%mon%%day%"
set "dy=%day:~1%"
if %day:~0,1% equ 0 set "day=%day:~1%"
if %mon:~0,1% equ 0 set "mon=%mon:~1%"
set months=January February March April May June^
 July August September October November December
for /f "tokens=%mon%" %%a in ("%months%") do set "month=%%a"
if /i %dy% equ 0 set "st=th"
if /i %dy% gtr 3 set "st=th"
if /i %day% geq 11 if /i %day% leq 13 set "st=th"
if defined st goto :end
set/a rst=%day% %% 10
for /f "tokens=%rst%" %%s in ("st nd rd") do set "st=%%s"
:end
set "dow=%dow%, %month% %day%%st%, %year%"
EndLocal&call set %~1=%dow%&call set %~3=%month%&exit /b %ymd%

:_GetString.cmd
set p0=%0&set "p0=!p0:~1!"
if not exist _GetString.cmd (
  call :checkpath %p0%
  if !errorlevel! neq 0 (
    set/p %~2="%~1"
    exit/b 0
  )
)
call _GetString.cmd "%~1" %~2 %~3
exit/b %errorlevel%

:checkpath
if exist "%~$PATH:1" exit/b 0
exit/b 1

:RegDateFmt Format Separator y m d dte
if "%~2"=="" exit/b 1
setlocal EnabledelayedExpansion
set "Day="&set "Mon="&set "dte="
if not "%~6"=="" set "dte=!%~6!"
if not defined dte set "dte=%date%"
for /f "tokens=2 delims=:" %%A in ('date^<nul') do set Format=%%A&goto :next
:next
set "Format=%Format: =%"
for %%A in (/ - . ,) do (
  echo %Format%|find "%%A">nul&if !errorlevel! equ 0 set "Separator=%%A"
)
if /i %Format:~0,1% gtr 9 set "Day=Day "
for /f "skip=1 tokens=2-4 delims=(-)" %%a in ('date^<nul') do (
  set "Format=%Day%%%a%Separator%%%b%Separator%%%c"
  for /f "tokens=1-3 delims=.-/ " %%d in ("%dte:* =%") do (
    set %%a=%%d&set %%b=%%e&set "%%c=%%f"
    echo !yy:~3!|find "ECHO">nul
    if /i "!errorlevel!" neq "0" set "Format=!Format:yy=yyyy!"
    if /i "!mm:~0,1!" gtr "9" (
      set "Format=!Format:mm=Mon!"
      call :month_convert mm
      if /i "!errorlevel!" neq "0" endlocal&exit/b 1
    )
  )
)
endlocal&(
  call set "%~1=%Format%"
  call set "%~2=%Separator%"
  if not "%~3"=="" call set "%~3=%yy%"
  if not "%~4"=="" call set "%~4=%mm%"
  if not "%~5"=="" call set "%~5=%dd%"
)&exit/b 0

:month_convert
set "months=Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec"
call set "month=%%%~1%%"
set/a mnum=0
for %%m in (%months%) do set/a mnum+=1&if /i %month% equ %%m call set "%~1=!mnum!"&exit /b 0
exit/b 1

:setRegDate dte format sep dow mon
setlocal EnableDelayedExpansion
if /i "%~5"=="" exit/b 1
set mon=!%~5!&set dow=!%~4!&set "dow=!dow:~0,3! "
set sep=!%~3!&set "format=!%~2!"&set "dte=!%~1!"
set yyyy=%dte:~0,4%&set yy=%dte:~2,2%&set mm=%dte:~4,2%&set "dd=%dte:~-2%"
set "toks=2-4"
echo %format%|find " ">nul
if %errorlevel% equ 1 set "dow="& set "toks=1-3"
for /f "tokens=%toks% delims=%sep% " %%a in ("%format%") do (
  set "dte=%dow%!%%a!%sep%!%%b!%sep%!%%c!"
)
endlocal&call set "%~1=%dte%"&exit/b 0 

Solution 5

I have an additional suggestion with robocopy:

@echo off &setlocal enabledelayedexpansion
set "day="
for /f "tokens=3,4,8skip=4delims=: " %%i in ('robocopy') do if not defined day (
    set "month=%%i"
    set "day=0%%j"
    set "year=%%k"
)
set /a cnt=0
for %%i in (Jan Feb Mar Apr May Jun Jul Aug Sep Oct Nov Dec) do (
    set /a cnt+=1
    if "%%i"=="%month%" set "month=0!cnt!"
)
set "day=%day:~-2%"
set "month=%month:~-2%"
echo.%day%.%month%.%year%
endlocal
Share:
17,231
sashoalm
Author by

sashoalm

Updated on July 15, 2022

Comments

  • sashoalm
    sashoalm almost 2 years

    Is there a way to split the %date% in a batch file (say, in 3 environment variables), but regardless of Regional Settings? Today's date would be 3/13/2013 for US, but with my Regional Settings it is 13.3.2013 - the delimiter is changed and the order as well.

  • Endoro
    Endoro about 11 years
    There is no way to get a valid date without external binaries (e.g. wmic, robocopy) in batch but set the registry by yourself. The date and dircommands does always show sShortDatefrom the registry's international settings.
  • Endoro
    Endoro about 11 years
    This does not work, if the user sets the date format in the windows control panel to e.g. dd yy (output from your code is: 13130).
  • sashoalm
    sashoalm about 11 years
    What is the suppressed-leading-zero situation?
  • Magoo
    Magoo about 11 years
    @mfm4aa That's because dd yy is a format thay you can set, but makes no sense.
  • sashoalm
    sashoalm about 11 years
    Thanks for the answer. Could you describe your approach a bit, though? For example, is it using external utilities, etc.
  • Endoro
    Endoro about 11 years
    Please set the date format in the windows contol panel to dd yy and you will see, this doesn't work (output: Standard date: 13 03//). Every user can set the date format to whatever he wants. The date command depends on that setting (also in redirection).
  • Magoo
    Magoo about 11 years
    @sashoalm The format use for the %date% variable is the short date format. Control Panel>Region and Language>Additional Settings>Date tab - you may enter the format that will be reported. If you enter d then the day number is shown as a single-digit for 1..9. If you enter dd then a leading-zero will be added for 1..9. Ditto for month with m against mm. Different people use different formats...
  • Endoro
    Endoro about 11 years
    You can set it to mm yy and it will not work. Your code is not independent from 'regional settings'. You always need a compliant user.
  • Magoo
    Magoo about 11 years
    @mfm4aa That's because mm yy is also a format - no doubt one of many - that you can set, but makes no sense
  • Endoro
    Endoro about 11 years
    Users do things without sense all day long. OK, I think, you know what I mean.
  • Magoo
    Magoo about 11 years
    Well it nearly works. It doesn't work in the default situation where a dayname is present in %date%. Nor does it work properly as a universal format where the local year format is yy not yyyy or month m not mm or day d not dd For instance does the routine's result "13/3/12" mean 2013/03/12 or 2012/3/13 when interpreted by a human? Now - each of these problems CAN be solved - for instance, for/f "tokens=1-4"...%date%... would assign the fourth metavariable only if a dayname is present (& presumably the first) and the leading-zeroes could be applied with a bit of maths/substrings
  • Jesse Chisholm
    Jesse Chisholm almost 9 years
    Thank you for the link to "parsing the date command" (robvanderwoude.com/amb_datetime.php)
  • cyberponk
    cyberponk about 8 years
    If this does not work on your system, please tell me the output of echo:|date so I can update the code. Thanks!
  • Andrew Grothe
    Andrew Grothe over 6 years
    So clean. very useful.
  • Bricktop
    Bricktop over 4 years
    This changes the size and font of my command prompt window. How can I avoid that?
  • Federico Destefanis
    Federico Destefanis over 4 years
    @Bricktop your issue may be related to this superuser.com/questions/706562/…
  • Francois Bertrand
    Francois Bertrand over 2 years
    Way underappreciated response if this works across the board. Works here, however I get 4 digits for the year.