Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

The %DATE% and %TIME% environment variables provide the current date and time on Windows machines on the command line and inside a batch file.

Sadly, those values are locale-aware! Meaning that, say, on a German machine, you will get


instead of


this screws up sorting if you want to use this variable in a file name.

Is there any easy way to get hold of a locale-unaware YYYY-MM-DD date string in a Windows batch file?

For the moment, I am working with this rather kludgy workaround:

for /f "tokens=1,2,3,4 delims=. " %%i in ('date /t') do set date=%%k-%%j-%%i
echo %date%

but this is now German locale specific - obviously, a completely independent solution would be much nicer.

The OS in question is Server 2008. I would much prefer not using a third party tool, if at all possible.

share|improve this question

There were a few attempts (Rob van der Woude had something), but nothing really worked across all locales. However, you can get the current time in a easily-parseable format via

wmic os get LocalDateTime

The following gets you at least UTC already:

@echo off
rem Get the time from WMI - at least that's a format we can work with
set X=
for /f "skip=1 delims=" %%x in ('wmic os get localdatetime') do if not defined X set X=%%x

rem dissect into parts
set DATE.YEAR=%X:~0,4%
set DATE.MONTH=%X:~4,2%
set DATE.DAY=%X:~6,2%
set DATE.HOUR=%X:~8,2%
set DATE.MINUTE=%X:~10,2%
set DATE.SECOND=%X:~12,2%
set DATE.FRACTIONS=%X:~15,6%
set DATE.OFFSET=%X:~21,4%


However, you probably need to account for the time zone offset (unless it's for a log file, then I'd always use UTC) but that's nothing a bit of calculation cannot do :-)

share|improve this answer
Excellent, this looks exactly like what I need! Thank you. – Pekka 웃 Jan 26 '11 at 21:56
Win!. Thanks :) – CAD bloke Nov 26 '12 at 23:29
For the record: for /f %%x in ('wmic os get localdatetime ^| findstr /b [0-9]') do @set X=%%x is a tiny bit nicer. There's also a more compact version available over here, namely in that comment. – Tomalak Jan 22 '14 at 10:09
It's a bit hard finding and updating all old instances where you wrote something :). I'd probably use Win32_UTCTime and Win32_LocalTime by now. – Joey Jan 22 '14 at 11:57

Here's a two-liner I've been using, which seems to work regardless of Windows version or local time settings:

FOR /f %%a in ('WMIC OS GET LocalDateTime ^| find "."') DO set DTS=%%a
set CUR_DATE=%DTS:~0,4%-%DTS:~4,2%-%DTS:~6,2%

This will set a variable called %CUR_DATE% in the following ISO standard format:


I tried making it a one-liner with &&, but it didn't seem to take, so I resorted to the two-line version. Hope this helps.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.