How to get and display yesterday date?

2020-01-24 05:25发布

I am using the date command for a batch script.
I am wondering how to use command date to get yesterday date.

标签: batch-file
9条回答
Ridiculous、
2楼-- · 2020-01-24 05:55

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%
查看更多
劫难
3楼-- · 2020-01-24 05:56

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
查看更多
倾城 Initia
4楼-- · 2020-01-24 06:06

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.

查看更多
▲ chillily
5楼-- · 2020-01-24 06:12

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

查看更多
闹够了就滚
6楼-- · 2020-01-24 06:14

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
查看更多
Luminary・发光体
7楼-- · 2020-01-24 06:17

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...

查看更多
登录 后发表回答