Previous / yesterday’s date in DOS batch file

Here is the script to get previous date output in DOS using bat files. This is very useful when we take backup of files with previous date name. (for example ) or can set bat files to get last 7 days backup to a zip.  According to your requirement you can get any previous date from this script.  See the variable values. Following Script just show today’s and yesterdays date. Most of the script having some problems when it run on 1st if any moth. but here it is solved. you can customize the date formats also. using variables %DD%, %MM%, %YYYY%  Please comment to this post If you have any query .

@echo off

for /f "tokens=1" %%i in ('date /t') do set thedate=%%i

set mm=%thedate:~3,2%
set dd=%thedate:~0,2%
set yyyy=%thedate:~6,4%
echo Today : %dd%.%mm%.%yyyy%

if %dd%==08 (
set dd=8 ) else (
if %dd%==09 (
set dd=9 ) )

if %mm%==08 (
set mm=8 ) else (
if %mm%==09 (
set mm=9 ) )

set /A dd=%dd% - 1
set /A mm=%mm% + 0

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
if %mm%==12 goto SET31

goto ERROR

set /A dd=31 + %dd%
goto DONE

set /A dd=30 + %dd%
goto DONE

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%
goto DONE

set /A dd=29 + %dd%

if /i %dd% LSS 10 set dd=0%dd%
if /I %mm% LSS 10 set mm=0%mm%
set YESTERDAY=%dd%.%mm%.%yyyy%

About Albin Sebastian

I am a Technology Blogger, I Blog about technology related articles, Active in online and offline tech communities

Leave a Reply

More in Programming (5 of 10 articles)