Create an account

Very important

  • To access the important data of the forums, you must be active in each forum and especially in the leaks and database leaks section, send data and after sending the data and activity, data and important content will be opened and visible for you.
  • You will only see chat messages from people who are at or below your level.
  • More than 500,000 database leaks and millions of account leaks are waiting for you, so access and view with more activity.
  • Many important data are inactive and inaccessible for you, so open them with activity. (This will be done automatically)


Thread Rating:
  • 414 Vote(s) - 3.57 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get yesterday's date in batch file?

#1
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.
Reply

#2
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
Reply

#3
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
Reply

#4
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

[To see links please register here]

Reply

#5
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
Reply



Forum Jump:


Users browsing this thread:
1 Guest(s)

©0Day  2016 - 2023 | All Rights Reserved.  Made with    for the community. Connected through