Parsing Dates in Batch Files & Regional Settings / Locale

Determining day, month and year on the command line is surprisingly difficult if the solution is to work across locales, i.e. on international systems with different regional settings. Although we can use the variables %DATE% and %TIME% their contents is specific to the locale. If set to German, the date is displayed in the format dd.MM.yyyy, if set to English (United States), the date format is M/d/yyyy.

Searching the web I have found several solutions to this, the most elegant relying on WMI to return date and time independent of the locale. My script below is a modified version of of Rob van der Woude’s script SortDate.bat.

In the script, the WMI command-line tool WMIC is used to get year, month, day, hour, minute and second individually. Out of those individual values the formatted date and time strings are constructed. Unfortunately, WMI does not return the milliseconds, so that we are limited to a resolution of seconds.

The output from the script looks like this:


Of course, the actual formatting can be changed easily.

And here is the script FormattedDateTime.cmd:

@echo off
:: Get date/time independent of system locale and regional settings
:: By Helge Klein
:: Based on code by Rob van der Woude (
:: 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 FormattedDate=10000 * %%f + 100 * %%d + %%a
		set FormattedDate=!FormattedDate:~0,4!-!FormattedDate:~4,2!-!FormattedDate:~6,2!
		set /A FormattedTime=10000 * %%b + 100 * %%c + %%e
		set FormattedTime=0000000!FormattedTime!
		set FormattedTime=!FormattedTime:~-6,2!:!FormattedTime:~-4,2!:!FormattedTime:~-2,2!
:: Display the results
set formatted

, , ,

One Response to Parsing Dates in Batch Files & Regional Settings / Locale

  1. Björn October 26, 2014 at 18:41 #

    Works like a charm, thanks!

Leave a Reply