1、 批处理 bat 计算前 N 个月第一天的日期【方案一】BAT + date.exe(GNU)代码如下:echo offrem 调用 GNU for Win32 的 date.exeset “gnuDate=C:/test/date.exe“rem 指定月数set MonthsAgo=1for /f %a in (%gnuDate% -d “%MonthsAgo% months ago“ “+%Y-%m-01“) do (set “DstDate=%a“)echo,%DstDate%pause【方案二】BAT + VBS代码如下:echo offrem 指定月数set MonthsAgo=2“
2、%temp%/DateCalculate.vbs“ echo strLastDate=DateAdd(“m“, -%MonthsAgo%, Date)“%temp%/DateCalculate.vbs“ echo strFmtDate=Right(Year(strLastDate),4) ,2%echo,%DstDate%pauseamp; Month(strLastDate),2) ,2%echo,%DstDate%pauseamp; “01“%temp%/DateCalculate.vbs“ echo WScript.Echo strFmtDatefor /f %a in (cscript
3、 /nologo “%temp%/DateCalculate.vbs“) do (set “DstDate=%a“)set DstDate=%DstDate:0,4%-%DstDate:4,2%-%DstDate:6,2%echo,%DstDate%pauseamp; Right(“0“ ,2%echo,%DstDate%pauseamp; Month(strLastDate),2) ,2%echo,%DstDate%pauseamp; “01“%temp%/DateCalculate.vbs“ echo WScript.Echo strFmtDatefor /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