Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get yesterday's date in batch file? [duplicate]

I use below code to get today's date and month (0411)

For /f "tokens=2-4 delims=/ " %%a in ('date /t') do (set FileNameDatePrefix=%%a%%b)

But I want to get yesterday's date in FileNameDatePrefix.

I am not sure how we can do this. Any suggestions are welcome.

like image 606
Ishan Trikha Avatar asked Apr 11 '14 10:04

Ishan Trikha


1 Answers

Here is a batch file method using VBS which is robust in any locale

:: yesterdays date
@echo off
set day=-1
echo >"%temp%\%~n0.vbs" s=DateAdd("d",%day%,now) : d=weekday(s)
echo>>"%temp%\%~n0.vbs" WScript.Echo year(s)^& right(100+month(s),2)^& right(100+day(s),2)
for /f %%a in ('cscript /nologo "%temp%\%~n0.vbs"') do set "result=%%a"
del "%temp%\%~n0.vbs"
set "YYYY=%result:~0,4%"
set "MM=%result:~4,2%"
set "DD=%result:~6,2%"
set "data=%yyyy%-%mm%-%dd%"

echo Yesterday was "%data%"
pause
like image 61
foxidrive Avatar answered Sep 25 '22 06:09

foxidrive