Community Pick: Many members of our community have endorsed this article.
Editor's Choice: This article has been selected by our editors as an exceptional contribution.

Using dates in batch files / scripts

Steve KnightIT Consultancy
CERTIFIED EXPERT
Published:
Updated:

See my pages here: http://scripts.dragon-it.co.uk/ for more batch, VBScript, HTA etc.


Using dates in 'DOS' batch files has always been tricky as it has no built in ways of extracting date information.  There are many tricks using string manipulation to pull out parts of the %date% variable or output of the date /t command but these rely heavily on the regional settings of the user the script is running as.


If a quick and dirty date or time stamp for a filename, for instance, is required you can use some simple character substitution to amend the date / time entries to remove the / and : characters so that they can be used in a filename, e.g.


set filename="File %date:/=-%%time::=-%".txt

results in a filename like  "File 09-07-2009 9-37-42.40.txt"

or "File Thu 09-07-2009 9-37-42.40.txt"

or "File Thu 07-09-2009 9-37-42.40.txt"

or "File 07-09-2009 9-37-42.40.txt"

etc.

which is why extracting strings from a date is unreliable.


VBScript can be be used to extract day names, parts of the date etc. and combined into a batch file by using a for command to read the data ouput by a VB Script (amongst other ways).  This simple one line VBscript for instance returns all the various parts of a date that may be needed and can be used as in the example to construct a filename / path for instance:


wscript.echo weekdayname(weekday(date)) & "," & weekday(date) & "," & weekdayname(weekday(date),true) & "," & day(date) & "," & month(date) & "," & year(date) & "," & monthname(month(date),false) &"," & monthname(month(date),true) 


That could be called dateparts.vbs and stored somewhere for running or constructed on the fly from the batch file, e.g. in the user's temp folder.  All that is needed is to escape the & characters with a ^ and use redirection (>) to create a temporary VBS file:


@echo off
echo wscript.echo weekdayname(weekday(date)) ^& "," ^& weekday(date) ^& "," ^& weekdayname(weekday(date),true) ^& "," ^& day(date) ^& "," ^& month(date) ^& "," ^& year(date) ^& "," ^& monthname(month(date),false) ^&"," ^& monthname(month(date),true) > "%temp%\dateparts.vbs"


This can then be read using a for command taking the output from this file into environment variables:


for /f "tokens=1-8 delims=," %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set weekday=%%a& set weekdaynum=%%b& set shortday=%%c& set day=%%d& set month=%%e& set year=%%f& set monthname=%%g& set shortmonth=%%h


and displayed or used to make filenames, directories or compared to run differently depending upon the day -- e.g. run a full backup on a Friday etc.


echo Weekday:    %weekday%
echo weekdaynum: %weekdaynum%
echo shortday:   %shortday%
echo day:        %day%
echo month:      %month%
echo monthname:  %monthname%
echo shortmonth: %shortmonth%
echo year:       %year%
set filename="D:\files\%year%\%shortmonth%\Report for %day%-%month%-%year% (%weekday%).txt"
echo The filename is %filename%


If only certain parts are required the VBScript and/or batch file for command can be amended, e.g.


@echo off
echo wscript.echo weekday(date) ^& "," ^& day(date) ^& "," ^& month(date) ^& "," ^& year(date)  > "%temp%\dateparts.vbs"
for /f "tokens=1-4 delims=," %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set weekday=%& set day=%%d& set month=%%e& set year=%%f
if %weekday%==Fri echo It is Friday. & goto fullbackup
goto normalbackup
:fullbackup
 echo Backing up to Z:\backups\weekly\%year%-%month%-%day%.zip
 rem backup commands
goto :eof
:normalbackup
 echo Backing up to Z:\backups\daily\%year%-%month%-%day%.zip
 rem backup commands
goto :eof


Steve Knight

http://www.dragon-it.co.uk/ getdatebits.cmd.txt

13
16,400 Views
Steve KnightIT Consultancy
CERTIFIED EXPERT

Comments (9)

Steve KnightIT Consultancy
CERTIFIED EXPERT

Author

Commented:
Steve KnightIT Consultancy
CERTIFIED EXPERT

Author

Commented:
Some more prebuilt example scripts for common requests.
yesterday-yyyymmdd.cmd
tomorrow-yyyymmdd.cmd
yyyy-mm-dd-hh-mm.cmd
CERTIFIED EXPERT

Commented:
Here's another way using the wmic utility.  I'm not sure when this utility first appeared in Windows, but it works in XP and later versions afaik:

Run this directly from a command prompt:

for /f "delims= " %C in ('wmic.exe path Win32_LocalTime Get Day^,Month^,Year /Format:List 2^>nul ^| find "="') do  set current%C

Open in new window

Steve KnightIT Consultancy
CERTIFIED EXPERT

Author

Commented:
End of Last month:

@echo off
REM Use VBScript to get date.  This will get the first day of this month and take one off to get last day of last month
(echo eolm=dateserial^(year^(date^),month^(date^),1^)-1
echo wscript.echo year^(eolm^) ^& right^(100 + month^(eolm^),2^) ^& right^(100+day^(eolm^),2^)) > "%temp%\dateparts.vbs"
for /f "tokens=1 delims=" %%a in ('cscript //nologo "%temp%\dateparts.vbs"') do set yyyymmdd=%%a


echo End of last month to use in filename was %yyyymmdd%

pause

Open in new window

eolm.cmd
Bill PrewTest your restores, not your backups...
CERTIFIED EXPERT
Expert of the Year 2019
Distinguished Expert 2020

Commented:
And of course, one of my preferred choices for date time formatting and math is DOFF, check it out at the link below.  It is a small utility program that you would need to have available to your BAT scripts, but I just keep a copy in a "util" folder that I include in my PATH.  Makes things very easy after that:

for /f "tokens=*" %%A in ('doff _yyyymmdd_hhmiss') do set dt=%%A

Open in new window


To get yesterday date you can simply do:

for /f "tokens=*" %%A in ('doff _yyyymmdd_hhmiss -1d') do set dt=%%A

Open in new window



Link:

http://www.jfitz.com/dos/#DOFF

***** EDIT *****

Whoops, looks like the creator of that handy freeware utility let the domain go.  You can still access the prior content of that page to see a description at the Internet Wayback Machine project via the first link below, and download the utility itself via the second link below.

http://web.archive.org/web/20150912034516/www.jfitz.com/dos/index.html
http://web.archive.org/web/20150912034516/www.jfitz.com/dos/doff10.zip



ยปbp

View More

Have a question about something in this article? You can receive help directly from the article author. Sign up for a free trial to get started.

Get access with a 7-day free trial.
You Belong in the World's Smartest IT Community