Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get yesterday's date in a batch file

I know how to get today's date in Windows 7. here is the command that I am using:

%DATE:~6,4%%DATE:~3,2%%DATE:~0,2%

But I want to get yesterday, I do not know how.

like image 899
user1188125 Avatar asked Aug 09 '12 20:08

user1188125


People also ask

How do I find yesterday's date?

How do you get yesterdays' date using JavaScript? We use the setDate() method on yesterday , passing as parameter the current day minus one. Even if it's day 1 of the month, JavaScript is logical enough and it will point to the last day of the previous month.

What does %1 do in batch?

When used in a command line, script, or batch file, %1 is used to represent a variable or matched string. For example, in a Microsoft batch file, %1 can print what is entered after the batch file name.

How to get yesterday's date in cmd?

Comment 2: If you want to execute for /f %%a in ('cscript //nologo yester. vbs') do set yesterday=%%a directly in the cmd terminal, you should replace the %% by % , otherwise you will get the following error message: %%A was unexpected at this time .

How do I timestamp in CMD?

You can use $T for time and $D for date, as well as several other expansions. See also: https://ss64.com/nt/prompt.html. https://docs.microsoft.com/en-us/windows-server/administration/windows-commands/prompt.


2 Answers

If you're limited to just cmd.exe, then the other solutions, despite their size, are probably as good as you'll get. However, modern Windows (such as your Win7) ships with quite a few other tools which can do the job far easier.

Just create a VBScript yester.vbs script as follows:

d = date() - 1
wscript.echo year(d) * 10000 + month(d) * 100 + day(d)

Then you can call it from your cmd script with:

for /f %%a in ('cscript //nologo yester.vbs') do set yesterday=%%a

and the yesterday variable will be created in the form yyyymmdd for you to manipulate however you desire.

like image 167
paxdiablo Avatar answered Oct 31 '22 19:10

paxdiablo


Here's a solution that creates the earlierday.vbs file on the fly, uses it and deletes it afterwards.

It stores the result in the NewDate variable

This example calculates 1 day ago, but can easily calculate a date further back by changing the value of the Offset variable.

@echo off
set Offset=1

echo d = date() - WScript.Arguments.Item(0) > earlierday.vbs
echo wscript.echo year(d) * 10000 + month(d) * 100 + day(d) >> earlierday.vbs

for /f %%a in ('cscript //nologo earlierday.vbs %Offset%') do set NewDate=%%a

del earlierday.vbs    
echo %NewDate%
pause

You could refine this slightly by using %temp%\earlierday.vbs to create the file in the user's temp folder.

Credits to paxdiablo as this is a simple tweak on his earlier post.

EDIT: Here's something with a loop, close to what I actually need it to do. This will take 14 days off today's date and return that date. Then it will keep going back 7 days at a time until it gets to 35 days day ago.

@echo off
SETLOCAL EnableDelayedExpansion

set BackDaysFrom=14
Set BackDaysTo=35
Set BackDaysStep=7

echo d = date() - WScript.Arguments.Item(0) > earlierday.vbs
echo wscript.echo year(d) * 10000 + month(d) * 100 + day(d) >> earlierday.vbs

for /L %%i in (%BackDaysFrom%, %BackDaysStep%, %BackDaysTo%) do (
    for /f %%a in ('cscript //nologo earlierday.vbs %%i') do set NewDate=%%a
    echo !NewDate!
)

del earlierday.vbs    

pause
like image 23
Alex Wilson Avatar answered Oct 31 '22 20:10

Alex Wilson