开发者

How to get and display yesterday date?

I am using the date command for a batch script.

I am wo开发者_JAVA百科ndering how to use command date to get yesterday date.


The main danger with the date variable is the locale sensitivity. If you have PowerShell available (it's a lot more common these days even in the big corporations) then you can use PowerShell to do the formatting and wrap it within a batch FOR statement.

The following PowerShell line will do the maths and format the date for you:-

PowerShell $date = Get-Date; $date=$date.AddDays(-1); $date.ToString('yyyy-MM-dd')

You can then execute this via FOR to get it into a batch file variable (remembering to escape a whole bunch of characters with the hat ^ symbol, and using the backtick to avoid the embeded quotes):-

for /f "usebackq" %%i in (`PowerShell $date ^= Get-Date^; $date ^= $date.AddDays^(-1^)^; $date.ToString^('yyyy-MM-dd'^)`) do set YESTERDAY=%%i echo %YESTERDAY%

I'm sure someone with superior PowerShell and Batch programming skills can reduce the PowerShell command and/or the number of escaped characters to make it more readable/maintainable.


Anytime you hear batch, think Rob Van der Woude. Anyway, here's yesterday.bat.


Looking at @JRL's answer... If it's truly that hard, perhaps use PowerShell and then do similar to Powershell's Get-date: How to get Yesterday at 22:00 in a variable?

You can call to PowerShell in a bat file like so: Use bat to start Powershell script

You'll end up with a three or four liner solution rather than the 100 or so written (immaculately I'll add) by Rob Van der Woude.

Good luck...


There is a much cheaper way of doing this, exclusively in batch. I know its rough, but it worked for me.

Basically write yesterdays date into a text file like yesterday.txt. Then call it next time the process runs. Works for a process I have that runs once a day only.

::pick up yesterdays date from file
::Needs to be done as the file generated today is *yesterdays* report.
for /F "tokens=1" %%a IN (D:\BIN\Yesterday.txt) DO set yest=%%a

::Write todays date to file for use tomorrow
echo %date% >D:\BIN\Yesterday.txt

Then you can call yesterdays date as variable with %yest%.


I found this script to work well

@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%

The results would look like:

IIS Date: 130904
AWS Date: 2013-09-04

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


Simply create a batch file and add this:

@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 "result=%yyyy%-%mm%-%dd%"
REM Below Will echo full date including year, month and yesterday's date.
echo %result%
REM Below line will echo yesterday's date (Day only).
echo %DD%
pause


1) Here's a script called yesterday.bat:

 @if (@x)==(@y) @end /***** jscript comment ******
     @echo off

     cscript //E:JScript //nologo "%~f0"
     exit /b 0

 @if (@x)==(@y) @end ******  end comment *********/

var d = new Date();
d.setDate(d.getDate() - 1);

var mm=(d.getMonth())+1
if (mm<10){
  mm="0"+mm;
}
var dd=d.getDate();
if (dd<10) {
 dd="0"+dd;
}
WScript.Echo(d.getFullYear()+""+mm+""+dd);

you can use it like

for /f %%a in ('yesterday.bat') do set "ystd=%%a"

2) Here's a one-liner with powershell which you most probably have installed:

    powershell "[DateTime]::Today.AddDays(-1).ToString("""yyyyMMdd""")"

and you can assign this to variable:

    for /f %%a in ('powershell "[DateTime]::Today.AddDays(-1).ToString("""yyyyMMdd""")"') do set ystd=%%a


i accomplished yesterdays date as follows.

set m=%date:~-7,2%
set /A m -= 1
set DATE_DIR=%date:~-10,2%-%m%-%date:~-4,4%

the format can be changed in line 3

sample output: 03-13-2013

this is the simplest way i found to do this.


The EASIEST way to get yesterdays date (YYYYMMDD) in batch is the following:

set D=%date:~-10,2%
set /A D -= 1
echo %date:~-4,4%%date:~-7,2%%D%
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜