How can I create a date stamp for yesterday

前端 未结 2 1769
误落风尘
误落风尘 2020-12-22 05:51

I am trying to acquire the contents of the file with a datestamp in the file name for yesterday (WH.FBTBT20130214.csv). I created a date stamp variable for today but cannot

相关标签:
2条回答
  • 2020-12-22 06:08

    I found the answer. Just in case anyone is interested:

    Set /A DateStamp=(%date:~10,4%%date:~4,2%%date:~7,2%)-1

    echo DateStamp: %DateStamp%

    0 讨论(0)
  • 2020-12-22 06:17

    You can't always just subtract 1 from the date and get yesterday's date. Say it's March 1. What's yesterday? March 0? Doesn't work that way. A better way is to use vbscript's Date() object and subtract a day from it.

    @echo off
    setlocal
    REM ***** 20130215 MS Define DateStamp variable *****
    echo d = DateAdd^("d", -1, Date^(^)^)>yesterday.vbs
    echo wscript.echo DatePart^("yyyy", d^) ^& "/" ^& DatePart^("m", d^) ^& "/" ^& DatePart^("d", d^)>>yesterday.vbs
    for /f "tokens=1-3 delims=/" %%I in ('cscript /nologo yesterday.vbs') do (
        set Year=%%I
        if %%J LEQ 9 (set Month=0%%J) else set Month=%%J
        if %%K LEQ 9 (set Day=0%%K) else set Day=%%K
    )
    set DateStamp=%Year%%Month%%Day%
    del /q yesterday.vbs
    echo %DateStamp%
    
    0 讨论(0)
提交回复
热议问题