批处理bat计算前N个月第一天的日期
程序员文章站
2022-03-22 16:32:56
【方案一】bat + date.exe(gnu)
复制代码 代码如下:
@echo off
rem 调用gnu for win32的date....
【方案一】bat + date.exe(gnu)
复制代码 代码如下:
@echo off
rem 调用gnu for win32的date.exe
set "gnudate=c:/test/date.exe"
rem 指定月数
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 指定月数
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