Generate Yesterday’s Date Through Windows Batch

Generating past date in a windows batch can be tricky. There doesn’t seem to be any date arithmetic functionality within the DOS kernel. There are a number of ways you can do it, but the one that works for me is through the generation of a text file. There is only one pre-requisite which is listed below

  • Windows Script Hosting (Installed on most XP Pro machines but can be downloaded here.)

 

You need to create two files, in this example, I’ve referred to them as gendate.vbs and gendate.bat. Gendate.vbs is written in vbscript and it takes two integer parameters:

  • Days Integer Paramater eg. -1 for yesterday’s date, 1 for tomorrow’s date and 0 for today’s date
  • Date output format parameter i.e. 0 returns YYYY-MM-DD, 1 returns DD-MON-YY

 

The first parameter is an integer value which is the number of days before/after the current date – used to determine the date you want returned. The second parameter determines the date format you want returned i.e 0 returns YYYY-MM-DD format, while 1 returns DD-MON-YY format. This module can be extended to return whatever format you require. Here’s the content of gendate.vbs:

'Wscript.Arguments(0) - Days Integer Paramater eg. -1 for yesterday's date, 1 for tomorrow's date and 0 for today's date
'Wscript.Arguments(1) - Date output format parameter i.e. 0 returns YYYY-MM-DD, 1 returns DD-MON-YY

dateRequested = dateadd("d", Wscript.Arguments(0) ,date)
yestStr = dateToString(dateRequested, Wscript.Arguments(1))

'Write response to file
Dim oFs, oTextFile, iMode
iMode = 2
set oFs = createobject("Scripting.FileSystemObject")
set oTextFile = oFs.OpenTextFile("date.txt", iMode, True)
oTextFile.Write yestStr
oTextFile.Close
set oTextFile = nothing
set oFS = nothing

'**************************************************************************************
'* dateToString
'* -------------------------------
'* Returns date object as a string
'* if opt = 0 then returns date string in YYYY-MM-DD format
'* otherwise return DD-MON-YY
'**************************************************************************************
Function dateToString(dteObj, opt)
  dayStr = Day(dteObj)
  if CInt(dayStr) < 10 then
    dayStr = "0" & dayStr
  end if
  monStr = Month(dteObj)
  if CInt(monStr) < 10 then
    monStr = "0" & monStr
  end if

  if opt = 0 then
    retStr = Year(dteObj) & "-" & monStr & "-" & dayStr
  else
    monName = MonthName(monStr,true)
    retStr = dayStr & "-" & UCASE(monName) & "-" & Right(Year(dteObj), 2)
  end if
dateToString = retStr
End Function

Gendate.bat is windows batch file that drives the process. It calls gendate.vbs and outputs the date string requested. Of course you need to add your business process in this file so that it performs tasks that you require. Here’s the content of gendate.bat:

@echo off

if "%1"=="" goto :usage
if "%2"=="" goto :usage

REM If you're familiar with VBSCRIPT you can modify this file to return any date format that you require
gendate.vbs %1 %2

setlocal enabledelayedexpansion
set SEPARATOR=
set filecontent=
for /f "delims=" %%a in (date.txt) do (
  set currentline=%%a
  set filecontent=!filecontent!%SEPARATOR%!currentline!
)

set datestr=%filecontent%
echo The date requested is: %datestr%
echo This value is stored in a variable called "datestr" and can be used within the script

goto :done

:Usage

echo usage 		gendate [Days Integer Paramater] [Date format type]

echo Example to generate yesterday's date in YYYY-MM-DD format: 	gendate -1 0
echo Example to generate yesterday's date in DD-MON-YY format: 	gendate -1 1

echo Example to generate tomorrow's date in YYYY-MM-DD format: 	gendate -1 0
echo Example to generate tomorrow's date in DD-MON-YY format: 	gendate -1 1

echo Example to generate today's date in YYYY-MM-DD format: 	gendate 0 0
echo Example to generate today's date in DD-MON-YY format: 	gendate 0 1

:done

To use the script, call the following in a command prompt window:

echo Example to generate yesterday’s date in YYYY-MM-DD format: gendate -1 0

echo Example to generate yesterday’s date in DD-MON-YY format: gendate -1 1

echo Example to generate tomorrow’s date in YYYY-MM-DD format: gendate -1 0

echo Example to generate tomorrow’s date in DD-MON-YY format: gendate -1 1

echo Example to generate today’s date in YYYY-MM-DD format: gendate 0 0

echo Example to generate today’s date in DD-MON-YY format: gendate 0 1

You may also like...

Leave a Reply

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