Get Yesterday date in MS DOS Batch file

hocheeming used Ask the Experts™

How do I obtain the yesterday date for MSDOS batch?

I am curently doing the following which is faulty. I am not sure how I can deduce the date correctly. Please advise. Thanks.



for /F "tokens=1-4 delims=/- " %%a in ('date/T') do set DATE=%%d%%c%%b

for /F "tokens=1-4 delims=/- " %%a in ('date/T') do set /a DAY=%%b

set /a YESTERDAY = DATE - 1

echo %DAY%

if  "%DAY%" == "1" (
      set YESTERDAY=%DATE%
)else (
      set YESTERDAY = DATE - 1
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®


set /a YESTERDAY = DATE - 100

because last 2 bytes used for month, previous to that one is the date field

This one would work correctly

for /F "tokens=1-4 delims=/- " %%a in ('date/T') do set


for /F "tokens=1-4 delims=/- " %%a in ('date/T') do set /a


set /a YESTERDAY = DATE - 100

echo %DAY%

if  "%DAY%" == "1" (
)else (
     set YESTERDAY = DATE - 100


It does not work correctly.

My date format is yyyymmdd.

You cannot just minus off directly as the batch script need to cater for cases like 1st Mar. Which when upon deduction, it should not know that the value is 28 and not 0!
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

ok. In my system it's in yyyyddmm. that's y i put like that.

The problem u face is some what big. I don't thing 50 points is worth for this

@echo off
setlocal enabledelayedexpansion
:: *** This script runs only on XP
:: *** Get the current date into the variables dd, mm, yy:
for /f "tokens=1-3 delims=/" %%a in ("%date%") do (
  set dd=%%a
  set mm=%%b
  set yy=%%c
:: *** Remove leading zeroes for date calculation:
if %dd:~0,1%==0 set dd=%dd:~1,1%
if %mm:~0,1%==0 set mm=%mm:~1,1%
:: *** Create an array with the months' lengths:
set i=1
for %%a in (31 28 31 30 31 30 31 31 30 31 30 31) do (
  set Days[!i!]=%%a
  set /a i+=1
:: *** (crude) check for a leap year:
set /a Leap=yy %% 4
if %Leap%==0 set Days[2]=29
:: *** Check if today is the first and, if so, calculate overflow:
if %dd%==1 (
  set /a mm -= 1
  if !mm! LSS 1 (
    set /a yy -= 1
    set mm=12
if %dd%==1 (
  set dd=!Days[%mm%]!
) else (
  set /a dd -= 1
:: *** Add the leading zeroes again:
if %dd% LSS 10 set dd=0%dd%
if %mm% LSS 10 set mm=0%mm%
:: *** Put together the logfile's and, while we're at it, the PDF file's name:
set LogFile=log%mm%_%dd%_%yy%.txt
set PDFFile=%yy%_%mm%_%dd%_stats.pdf

echo Name of the log file: %LogFile%
echo Name of the PDF file: %PDFFile%

copy "\stats\%LogFile%" "\devnew\%LogFile%" look in to this for more
Top Expert 2007
Here's a batch file I developed to subtract any number of days from the current date. It accepts a command line parameter of the number of days. The default is 1 day (yesterday):

@echo off

set yyyy=

set $tok=1-3
for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%u
if "%$d1:~0,1%" GTR "9" set $tok=2-4
for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (
 for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (
    set %%x=%%u
    set %%y=%%v
    set %%z=%%w
    set $d1=
    set $tok=))

if "%yyyy%"=="" set yyyy=%yy%
if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% - 100

set CurDate=%mm%/%dd%/%yyyy%

set dayCnt=%1

if "%dayCnt%"=="" set dayCnt=1

REM Substract your days here
set /A dd=1%dd% - 100 - %dayCnt%
set /A mm=1%mm% - 100


if /I %dd% GTR 0 goto DONE

set /A mm=%mm% - 1

if /I %mm% GTR 0 goto ADJUSTDAY

set /A mm=12
set /A yyyy=%yyyy% - 1


if %mm%==1 goto SET31
if %mm%==2 goto LEAPCHK
if %mm%==3 goto SET31
if %mm%==4 goto SET30
if %mm%==5 goto SET31
if %mm%==6 goto SET30
if %mm%==7 goto SET31
if %mm%==8 goto SET31
if %mm%==9 goto SET30
if %mm%==10 goto SET31
if %mm%==11 goto SET30
REM ** Month 12 falls through


set /A dd=31 + %dd%



set /A dd=30 + %dd%



set /A tt=%yyyy% %% 4

if not %tt%==0 goto SET28

set /A tt=%yyyy% %% 100

if not %tt%==0 goto SET29

set /A tt=%yyyy% %% 400

if %tt%==0 goto SET29


set /A dd=28 + %dd%



set /A dd=29 + %dd%



if /I %mm% LSS 10 set mm=0%mm%
if /I %dd% LSS 10 set dd=0%dd%

echo Date %dayCnt% day(s) before %CurDate% is %mm%/%dd%/%yyyy%

Good Luck,

Thank you SteveGTR for this answer, I have looking for such an answer for more than a month and nobody could do this so simple as you.
By the way, your ranking is really more than fair. ;-)
Steve you are awesome!!  This was just the code I was looking for too!!!

Great Job Steve. This works perfectly!!!
Hi Steve,

Copied your script into a file called GetYesterday.bat and from command line call it with:

>GetYesterday.bat 07/20/2011 I get back the same date (07/20/2011)

>GetYesterday.bat 2011/07/20 I get back 07/06/2011

>GetYesterday.bat 2011/01/01 I get back 01/16/2006

What am I doing wrong?  Does my OS version matter?   When I type "ver" at the command prompt I get "Microsoft Windows [Version 5.2.3790]"

Thanx Steve... Wonderful and easy to use script.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial