DOS/Windows batch file to define today date variable

Method one: 

It is very often that a batch file need to access date variable so that we can rename file to date format.  The below batch file code can be used to get system date and store as %dtt%


The below sample will generate yyymmdd_Day_hhmm under UK date format.

For the date variable, it breaks the output of date /t command into four tokens: %%i, %%j, %%k and %%l.  You can then arrange the order of the variable after set dt= command.


For the time variable, it breaks the output of time /t command into two tokens: %%i and %%j.  You can then arrange the order of the variable after set set tm= command.  You may also add extra formatting  e.g. colon ":", such as %%i:%%j, so to have hh:mm formating

If you want to have other format, or your Windows is of other date format, simply change the order of %%k, %%j and %%i after set dt= command.


for /f "Tokens=1-4 Delims=/ " %%i in ('date /t') do set dt=%%k%%j%%i_%%l
for /f "Tokens=1-2 Delims=: " %%i in ('time /t') do set tm=-%%i%%j
set dtt=%dt%%tm%
echo %dtt%

Method two:

(Assume %date% return mm/dd/yyyy)

set date=20%date:~8,2%%date:~3,2%%date:~0,2%
set time=%time:~0,2%%time:~3,2%





Comments