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

sashoalm picture sashoalm · Mar 13, 2013 · Viewed 16.4k times · Source

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.

Answer

Federico Destefanis picture Federico Destefanis · Aug 29, 2017

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