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
26.1.2011
instead of
2011-26-01
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.
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
The following gets you at least UTC already:
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 :-)
Here's a two-liner I've been using, which seems to work regardless of Windows version or local time settings:
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.
I use this trick to get UTC..
However it does have the side effect of briefly changing the entire system to UTC. This is probably OK on a personal workstation, but not sure what the impact would be a large production server, which should probably be set to UTC anyway.
Here is my solution to this problem:
Of course, the same as UTC time:
You may play with formatting as much as you like, the output is stable independent from Windows of CurrentLocale (here is minimal knowledge of Powershell and .Net required).