7

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.

Holger Just
  • 52,918
  • 14
  • 115
  • 123
Ishan Trikha
  • 251
  • 5
  • 6
  • 13

4 Answers4

24

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
foxidrive
  • 40,353
  • 10
  • 53
  • 68
3

Here is one way:

@echo off
setlocal

Call :GetDateTime Year Month Day
Echo %Year% %Month% %Day%
Call :AddSubtractDate %Year% %Month% %Day% -1 Ret
echo %Ret%
exit /b


:AddSubtractDate Year Month Day <+/-Days> Ret
::Adapted from DosTips Functions::
setlocal & set a=%4
set "yy=%~1"&set "mm=%~2"&set "dd=%~3"
set /a "yy=10000%yy% %%10000,mm=100%mm% %% 100,dd=100%dd% %% 100"
if %yy% LSS 100 set /a yy+=2000 &rem Adds 2000 to two digit years
set /a JD=dd-32075+1461*(yy+4800+(mm-14)/12)/4+367*(mm-2-(mm-14)/12*12)/12-3*((yy+4900+(mm-14)/12)/100)/4
if %a:~0,1% equ + (set /a JD=%JD%+%a:~1%) else set /a JD=%JD%-%a:~1%
set /a L= %JD%+68569,     N= 4*L/146097, L= L-(146097*N+3)/4, I= 4000*(L+1)/1461001
set /a L= L-1461*I/4+31, J= 80*L/2447,  K= L-2447*J/80,      L= J/11
set /a J= J+2-12*L,      I= 100*(N-49)+I+L
set /a YYYY= I, MM=100+J, DD=100+K
set MM=%MM:~-2% & set DD=%DD:~-2%
set ret=%YYYY: =%%MM: =%%DD: =%
endlocal & set %~5=%ret%
exit /b

:GetDateTime Year Month Day Hour Minute Second
@echo off & setlocal
for /f "tokens=2 delims==" %%a in ('wmic OS Get localdatetime /value') do set "dt=%%a"
set "YY=%dt:~2,2%" & set "YYYY=%dt:~0,4%" & set "MM=%dt:~4,2%" & set "DD=%dt:~6,2%"
set "HH=%dt:~8,2%" & set "Min=%dt:~10,2%" & set "Sec=%dt:~12,2%"
( ENDLOCAL
     IF "%~1" NEQ "" set "%~1=%YYYY%" 
     IF "%~2" NEQ "" set "%~2=%MM%" 
     IF "%~3" NEQ "" set "%~3=%DD%"
     IF "%~4" NEQ "" set "%~4=%HH%" 
     IF "%~5" NEQ "" set "%~5=%Min%"
     IF "%~6" NEQ "" set "%~6=%Sec%"
)
exit /b
Matt Williamson
  • 6,947
  • 1
  • 23
  • 36
2

This code will only work on Vista and later, as robocopy is used to retrieve the date of today. But will avoid the problem of locale formats in the %date% variable, the administrator requirement of wmic and the temporary file of VBScript. Anyway, the subroutine that gets the today date can be replaced with any other code. The :getYesterdayDate will handle all the calcs to substract one day from today.

@echo off
setlocal enableextensions enabledelayedexpansion

    call :getYesterdayDate yesterday
    echo %yesterday%

    exit /b 

:getYesterdayDate returnVar
    setlocal enableextensions disabledelayedexpansion
    call :getTodayDate today
    for /f "tokens=1-3 delims=/ " %%a in ("%today%") do set /a "y=%%a", "m=1%%b-100", "d=1%%c-100"
    if %d% gtr 1 ( set /a "d-=1" ) else (
        if %m% equ 1 ( set /a "y-=1" , "m=12" ) else ( set /a "m-=1" )
        set /a "d=30+((m+m/8) %% 2)"
        if %m%==3 set /a "d=d-2+!(y%%4)-!(y%%100)+!(y%%400)"
    )
    set "d=0%d%" & set "m=0%m%"
    endlocal & set "%~1=%y%/%m:~-2%/%d:~-2%" & exit /b

:getTodayDate returnVar
    setlocal enableextensions disabledelayedexpansion
    set "today=" & for /f %%a in ('robocopy "|" . /njh') do if not defined today set "today=%%a"
    endlocal & set "%~1=%today%" & exit /b
MC ND
  • 69,615
  • 8
  • 84
  • 126
  • A true gem. I had to remove the year part and forward slashes from the last line of getYesterdayDate subroutine in order to fit my date formatting requirements but other than that it just worked like a charm. – Piotr L Sep 08 '16 at 11:26
  • No need to rely on Robocopy, I could amend this to work on NT 5+ if preferred, as I created a simple for loop code long ago to get the date in a region independent manner with and without padding 0s. – Ben Personick Jan 04 '18 at 17:20
-1

You could try this:

@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

:CHKDAY
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

:ADJUSTDAY
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

:SET31
set /A dd=31 + %dd%
goto CHKDAY

:SET30
set /A dd=30 + %dd%
goto CHKDAY

:LEAPCHK
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

:SET28
set /A dd=28 + %dd%
goto CHKDAY

:SET29
set /A dd=29 + %dd%
goto CHKDAY

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

REM Set IIS and AWS date variables
set IISDT=%yyyy:~2,2%%mm%%dd%
set AWSDT=%yyyy%-%mm%-%dd%
echo %IISDT%
echo %AWSDT%
pause

Source http://www.powercram.com/2010/07/get-yesterdays-date-in-ms-dos-batch.html

09stephenb
  • 9,358
  • 15
  • 53
  • 91