how to get yesterday's date in a batch file

前端 未结 4 1474
时光取名叫无心
时光取名叫无心 2020-11-30 11:58

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 yes

4条回答
  •  有刺的猬
    2020-11-30 12:32

    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
    

提交回复
热议问题