how to get yesterday's date in a batch file how to get yesterday's date in a batch file windows windows

how to get yesterday's date in a batch file


If you're limited to just cmd.exe, then the other solutions, despite their size, are probably as good as you'll get. However, modern Windows (such as your Win7) ships with quite a few other tools which can do the job far easier.

Just create a VBScript yester.vbs script as follows:

d = date() - 1wscript.echo year(d) * 10000 + month(d) * 100 + day(d)

Then you can call it from your cmd script with:

for /f %%a in ('cscript //nologo yester.vbs') do set yesterday=%%a

and the yesterday variable will be created in the form yyyymmdd for you to manipulate however you desire.


Found a script that will work to ensure you get the previous day even if the year or month changes Dos Yesterday Batch.

@echo offset yyyy=set $tok=1-3for /f "tokens=1 delims=.:/-, " %%u in ('date /t') do set $d1=%%uif "%$d1:~0,1%" GTR "9" set $tok=2-4for /f "tokens=%$tok% delims=.:/-, " %%u in ('date /t') do (for /f "skip=1 tokens=2-4 delims=/-,()." %%x in ('echo.^|date') do (set %%x=%%uset %%y=%%vset %%z=%%wset $d1=set $tok=))if "%yyyy%"=="" set yyyy=%yy%if /I %yyyy% LSS 100 set /A yyyy=2000 + 1%yyyy% - 100set CurDate=%mm%/%dd%/%yyyy%set dayCnt=%1if "%dayCnt%"=="" set dayCnt=1REM Substract your days hereset /A dd=1%dd% - 100 - %dayCnt%set /A mm=1%mm% - 100:CHKDAYif /I %dd% GTR 0 goto DONEset /A mm=%mm% - 1if /I %mm% GTR 0 goto ADJUSTDAYset /A mm=12set /A yyyy=%yyyy% - 1:ADJUSTDAYif %mm%==1 goto SET31if %mm%==2 goto LEAPCHKif %mm%==3 goto SET31if %mm%==4 goto SET30if %mm%==5 goto SET31if %mm%==6 goto SET30if %mm%==7 goto SET31if %mm%==8 goto SET31if %mm%==9 goto SET30if %mm%==10 goto SET31if %mm%==11 goto SET30REM ** Month 12 falls through:SET31set /A dd=31 + %dd%goto CHKDAY:SET30set /A dd=30 + %dd%goto CHKDAY:LEAPCHKset /A tt=%yyyy% %% 4if not %tt%==0 goto SET28set /A tt=%yyyy% %% 100if not %tt%==0 goto SET29set /A tt=%yyyy% %% 400if %tt%==0 goto SET29:SET28set /A dd=28 + %dd%goto CHKDAY:SET29set /A dd=29 + %dd%goto CHKDAY:DONEif /I %mm% LSS 10 set mm=0%mm%if /I %dd% LSS 10 set dd=0%dd%REM Set IIS and AWS date variablesset IISDT=%yyyy:~2,2%%mm%%dd%set AWSDT=%yyyy%-%mm%-%dd%


@echo off:: Strip the day of the week from the current dateFOR %%A IN (%Date%) DO SET Today=%%A:: Parse the date, prefix day and month with an extra leading zeroFOR /F "tokens=1-3 delims=/" %%A IN ("%Today%") DO (    SET Day=0%%A    SET Month=0%%B    SET Year=%%C):: Remove excess leading zeroesSET Day=%Day:~-2%SET Month=%Month:~-2%:: Display the resultsSET DaySET MonthSET Year:: Convert to Julian dateCALL :JDate %Year% %Month% %Day%:: Display the resultSET JDate:: Subtract 1 daySET /A JPast = JDate - 1:: Display the resultSET JPast:: Convert back to "normal" date againCALL :GDate %JPast%:: Display the result::SET GDate=20130121SET GDateecho The previous day in form YYYYMMDD is %GDate%pause::::::::::::::::::::::::::::::::::::::::::::::::::::::GOTO:EOF:JDate:: Convert date to Julian:: Arguments : YYYY MM DD:: Returns   : Julian date:::: First strip leading zeroesSET MM=%2SET DD=%3IF %MM:~0,1% EQU 0 SET MM=%MM:~1%IF %DD:~0,1% EQU 0 SET DD=%DD:~1%:::: Algorithm based on Fliegel-Van Flandern:: algorithm from the Astronomical Almanac,:: provided by Doctor Fenton on the Math Forum:: (http://mathforum.org/library/drmath/view/51907.html),:: and converted to batch code by Ron Bakowski.SET /A Month1 = ( %MM% - 14 ) / 12SET /A Year1  = %1 + 4800SET /A JDate  = 1461 * ( %Year1% + %Month1% ) / 4 + 367 * ( %MM% - 2 -12 * %      Month1% ) / 12 - ( 3 * ( ( %Year1% + %Month1% + 100 ) / 100 ) ) / 4 + %DD% - 32075SET Month1=SET Year1=GOTO:EOF:GDate:: Convert Julian date back to "normal" Gregorian date:: Argument : Julian date  :: Returns  : YYYY MM DD:: :: Algorithm based on Fliegel-Van Flandern:: algorithm from the Astronomical Almanac,:: provided by Doctor Fenton on the Math Forum:: (http://mathforum.org/library/drmath/view/51907.html),:: and converted to batch code by Ron Bakowski.::SET /A P      = %1 + 68569SET /A Q      = 4 * %P% / 146097SET /A R      = %P% - ( 146097 * %Q% +3 ) / 4SET /A S      = 4000 * ( %R% + 1 ) / 1461001SET /A T      = %R% - 1461 * %S% / 4 + 31SET /A U      = 80 * %T% / 2447SET /A V      = %U% / 11SET /A GYear  = 100 * ( %Q% - 49 ) + %S% + %V%SET /A GMonth = %U% + 2 - 12 * %V%SET /A GDay   = %T% - 2447 * %U% / 80:: Clean up the messFOR %%A IN (P Q R S T U V) DO SET %%A=:: Add leading zeroesIF 1%GMonth% LSS 20 SET GMonth=0%GMonth%IF 1%GDay%   LSS 20 SET GDay=0%GDay%:: Return value:: Here you can define the form that you wantSET GDate=%GYear%%GMonth%%GDay%GOTO:EOF