How to get and show the date yesterday?

I use the date command for the script package.
I am wondering how to use the date command to get yesterday's date.

+3
source share
8 answers

Looking at @JRL's answer ... If it's really that complicated, maybe use PowerShell and then do the same Powershell's Date Received: How to get Yesterday at 22:00 in a variable?

You can call PowerShell in the bat file, for example: Use bat to run the Powershell script

In the end, you will have three or four solutions for the lineup, and not 100 or so (immaculately add) Rob van der Vude.

Good luck ...

+3
source

The main danger with a variable date is local sensitivity. If you have PowerShell (which is much more common these days even in large corporations), you can use PowerShell to format and transfer it to the batch FOR statement.

The next line of PowerShell will do the math and format the date for you: -

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

Then you can do this via FOR to get it in the variable of the batch file (remembering to avoid the whole group of characters with the hat ^ symbol and use the return line to avoid the enclosed 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 excellent PowerShell and Batch skills can reduce the PowerShell command and / or the number of escaped characters to make it more readable / supported.

+6
source

Anytime you hear a part, think Rob Van der Voude. Anyway, here yesterday .

+4
source

There is a much cheaper way to do this, exclusively in a batch. I know him roughly, but it worked for me.

In principle, write the date to a date in a text file, for example, yesterday.txt . Then call it the next time the process starts. Works for a process that I have that runs once a day.

 ::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 name the date yesterday as a variable with %yest% .

+2
source

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 look like this:

 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

+2
source

I fulfilled yesterday's date as follows.

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

format can be changed in line 3

sample output: 03-13-2013

This is the easiest way I have found for this.

+1
source

1) Here a script is 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 as

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

2) Here you will find a single-line layer with the power that you most likely installed:

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

and you can assign this to a variable:

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

The easiest way to get the date of the day (YYYYMMDD) in a package:

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

Source: https://habr.com/ru/post/922512/


All Articles