Date\Time Variable
To use a date and time variable in the Windows command line I have been using these commands:
for /f "tokens=1,2" %%u in ('date /t') do set d=%%v
for /f "tokens=1" %%u in ('time /t') do set t=%%u
if "%t:~1,1%"==":" set t=0%t%
set timestr=%d:~6,4%%d:~0,2%%d:~3,2%%t:~0,2%%t:~3,2%
To use this simply insert the variable %timestr% into your script wherever you want the date.
Military Time Problem and Solution
Unfortunately in order to have the output in military time you must set the computer clock to military time. I like having my clock display the standard time (AM\PM) but I want my time stamps to be in military time. so I now use a utility called doff (date offset) by John Fitzgibbon.
I found this handy utility because I extract HL7 data from Oracle databases going back a fixed number of days.
This flexible utility easily parses the date and time and uses 24 hour military time. Here is a sample script: doff_sample.txt
Get it here: http://www.jfitz.com/dos/index.html#DOFF
Powershell
Windows Powershell is a bit easier, but is only included with Windows Server 2008, so it must be deployed with .Net 2.0 before it can be used.
Create the function:
Function timestamp {
(get-date).toString('yyyyMMddHHmmss')
}
The variable (function) is now:
$timestamp
For the hour use HH for military time and hh for standard time.