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.
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.
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.
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 .
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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With