Creating a timestamp string in DOS

I found a better way of doing this using the DOS variables %DATE% and %TIME%

To extract the elements individually you can do:

set DD=%DATE:~0,2%
set MM=%DATE:~3,2%
set YYYY=%DATE:~6,4%
set HH=%TIME:~0,2%
set MI=%TIME:~3,2%
set SS=%TIME:~6,2%

….or to do it in one hit:

set TIMESTAMP=%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%^
%TIME:~0,2%%TIME:~3,2%%TIME:~6,2%
echo %TIMESTAMP%

TIMESTAMP will then look like this:

20080811100011

Note: I only just noticed, somewhat shamefully, that if you do this before 10:00am, you get a space in the timestamp, because the hour is represented as ‘ 9′, rather than ’09’. When I figure out a workaround I’ll post it.Update: this works: http://serverfault.com/a/220462

Advertisements