批處理bat計(jì)算前n個(gè)月第一天的日期

字號(hào):


    【方案一】bat + date.exe(gnu)
    代碼如下:
    @echo off
    rem 調(diào)用gnu for win32的date.exe
    set gnudate=c:/test/date.exe
    rem 指定月數(shù)
    set monthsago=1
    for /f %%a in ('%gnudate% -d %monthsago% months ago +%%y-%%m-01') do (
        set dstdate=%%a
        )
    )
    echo,%dstdate%
    pause
    【方案二】bat + vbs
    代碼如下:
    @echo off
    rem 指定月數(shù)
    set monthsago=2
    >%temp%/datecalculate.vbs echo strlastdate=dateadd(m, -%monthsago%, date)
    >>%temp%/datecalculate.vbs echo strfmtdate=right(year(strlastdate),4) ^& right(0 ^& month(strlastdate),2) ^& 01
    >>%temp%/datecalculate.vbs echo wscript.echo strfmtdate
    for /f %%a in ('cscript /nologo %temp%/datecalculate.vbs') do (
        set dstdate=%%a
    )
    set dstdate=%dstdate:~0,4%-%dstdate:~4,2%-%dstdate:~6,2%
    echo,%dstdate%
    pause