Easy Formatted UTC Dates from the Command line – PC

I’ve been using the command line to backup my files for a long time, but always used the date and time tools that ship with windows. the way I used to do it was like this (Sorry, long lines won’t wrap).

for /f "tokens=1,2,3,4* delims=/ " %%a in ('date /t') do set newdate=%%c%%b%%a
for /f "tokens=1,2,3,4* delims=: " %%a in ('time /t') do set newtime=%%a%%b
SET timestamp=%newdate%%newtime%
ECHO %timestamp%

This was delimiting the results from the date with a slash and delimiting the time with a colon.

This approach always had two problems for me:

  • The date and time was always the local system time
  • The date format (DD/MM or MM/DD) was based on the user running the script
  • Some locales format the date and time with different delimiters

Anyway, I finally decided I had enough with this solution and wrote a quick and dirty console application in C#.

It’s called datetimeUTC. Download it from here – If you install it in c:\windows\system32 you can call it from anywhere on the command line. It’ll need the .NET framework too.

Just call it without any arguments for the help. This is what you’ll see…

Returns a UTC date time value to a specified format

datetimeUTC [format]

Example formats:
yyyyMMddTHHmmssZ                20081121T100227Z
"yyyy-MM-dd HH:mm:ss UTC"       2008-11-21 10:02:27 UTC
"dd MMMM yyyy"                  21 November 2008
ddd                             Fri
dddd                            Friday
fff                             (Milliseconds)

Use double quotes when the format includes a space
Use backslashes to escape reserved characters

By: John Burns (www.john.geek.nz)
License: Free for any use


Known bugs

  • You can’t use double quotes in the middle of the format.
  • You cannot get a backslash in the output as its the escape character

In a batch file, you can get the output from it like this.

for /f "usebackq" %%a in (`datetimeUTC.exe yyyyMMddTHHmmssZ`) do (set UTCdts=%%a%)

Using the above script allows you to compress backups using an accurate UTC time stamp.

I’ll write another post soon about how I compress and auto email the files.

You May Also Like

About the Author: John

Leave a Reply

Your email address will not be published. Required fields are marked *