1

This is for current date, How do we use yesterday date ....?

@echo off
set YYYYMMDD=%DATE:~10,4%%DATE:~4,2%%DATE:~7,2% 
set a=%YYYYMMDD%
echo %a%
npocmaka
  • 55,367
  • 18
  • 148
  • 187
  • 5
    Possible duplicate of [How to get and display yesterday date?](https://stackoverflow.com/questions/2954359/how-to-get-and-display-yesterday-date) – wici Jan 26 '18 at 14:30
  • @wici Duplicate question, but don't really like the Link-only answer that was accepted. Also, OP is looking at doing this with mostly batch, it seems. – Gerhard Jan 26 '18 at 14:44
  • note that `%date%` is [**not a reliable way to get date**](https://stackoverflow.com/q/15378719/995714) – phuclv Jan 27 '18 at 06:37

3 Answers3

3

Try this shorter method (and the only pure-Batch solution in this topic):

@echo off
setlocal EnableDelayedExpansion

set /A "YYYY=%DATE:~10,4%, MM=1%DATE:~4,2%-100, DD=1%DATE:~7,2%-101, Feb=28+^!(YYYY%%4)"

set "DPM= 31 31 %Feb% 31 30 31 30 31 31 30 31 30"
if %DD% equ 0 set /A "MM+=M=-1,DD=0%DPM: =+^!(MM-(M+=1))*%,YYYY-=^!MM,MM+=12*^!MM"
set /A "MM+=100,DD+=100"

set "a=%YYYY%%MM:~1%%DD:~1%"
echo %a%

If you want to know what happens here, remove the @echo off line and run the program...

If you still have doubts about the method used, then you may do several tests over += and ! operators, and how the parentheses work.

Note that this solution does not work to subtract a number of days different than one. However, it is not difficult to insert the required adjustments to do that.

Aacini
  • 65,180
  • 12
  • 72
  • 108
  • `Feb=28+^!(YYYY%%4)` is not technically correct but it'll work for the next 81 years – phuclv Jan 27 '18 at 15:22
  • 1
    @LưuVĩnhPhúc: Well, yes, but I thought this was the last point someone would do about my code! **`;)`**. To make it "technically correct" for year 2100 just use this formula: `Feb=28+^!(YYYY%%4)-^!(YYYY%%100)`, and to extend this for year 2400 (and beyond) use: `Feb=28+^!(YYYY%%4)-^!(YYYY%%100)+^!(YYYY%%400)` – Aacini Jan 27 '18 at 19:20
  • It would be even better if you provided an explanation of what your script is doing – user2441441 Jan 08 '21 at 14:56
  • @user2441441: Why don't just test it and see by yourself? **`;)`** (the answer is Yes) – Aacini Jan 09 '21 at 05:55
0

Something like this should do.

@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%"
echo %result%
pause

Obviously by increasing set day=-1 to other numbers will deduct more days.

Simply double click the batch or run from cmd.exe prompt.

Gerhard
  • 22,678
  • 7
  • 27
  • 43
  • I thing you'll like this technique - https://stackoverflow.com/a/9074483/388389 . It makes the vbscript code easier to read and faster executed as there's writing to an additional file. – npocmaka Jan 26 '18 at 14:49
0
  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"
  1. 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
    
phuclv
  • 37,963
  • 15
  • 156
  • 475
npocmaka
  • 55,367
  • 18
  • 148
  • 187