I need to set Today and yesterday's date in a variable in a fixed format YYYYMMDD
.
For today date, when i did
SET TODAY=%date:~10,4%%date:~4,2%%date:~7,2%
it worked and displayed '20190426'.
But how to set yesterday's date so I get it in the format - 20190425
?
Update The original unix and linux tags were later changed to cmd and batch-file, which this Linux / Bash /
sh
solution won't apply to.To get yesterday's date:
To get it into a var:
Previously posted answers are not pure Batch-file solutions... You may use the method explained at this answer or just use this simpler approach:
There are literally hundreds/thousands of questions just here on SO.
I suggest you use a PowerShell one-liner for this, which you can call from a batch file as follows:
%YESTERDAY%
will then contain20190824
when invoked on 25 August 2019, for instance.A slightly longer variant, incorporating both
today
andyesterday
in only one PowerShell invocation.The PowerShell part issues two lines
which are parsed by the
for /f
and set as environment variablesyesterday
/today
respectivly.You can use this batch/vbs hybrid, you need to save it as a
.bat
or.cmd
extension file.:Note, you can toggle the number of days in the
set day=-1
line.