在线时间:8:00-16:00
迪恩网络APP
随时随地掌握行业动态
扫描二维码
关注迪恩网络微信公众号
【方案一】BAT + date.exe(GNU) 复制代码 代码如下: @echo off rem 调用GNU for Win32的date.exe set "gnuDate=C:/test/date.exe" for /f %%a in ('%gnuDate% "+%%Y%%m01"') do ( for /f %%h in ('%gnuDate% -d "%%a 1 days ago" "+%%Y-%%m-%%d"') do ( set "DstDate=%%h" ) ) echo,%DstDate% pause 【方案二】BAT + VBS 复制代码 代码如下: @echo off >"%temp%/DateCalculate.vbs" echo strLastDate=DateAdd("d", -DatePart("d", Date), Date) >>"%temp%/DateCalculate.vbs" echo strFmtDate=Right(Year(strLastDate),4) ^& Right("0" ^& Month(strLastDate),2) ^& Right("0" ^& Day(strLastDate),2) >>"%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 【方案三】BAT 复制代码 代码如下: @echo off setlocal enabledelayedexpansion rem 假设系统日期格式为yyyy-mm-dd set "y=%date:~0,4%" set "m=%date:~5,2%" set /a "m=1!m!-101, m=m+(^!m)*12" rem 上月是否2月或小月 set /a "f=^!(m-2), s=^!(m-4)|^!(m-6)|^!(m-9)|^!(m-11)" rem 今年是否闰年 set /a "leap=^!(y%%4) & ^!^!(y%%100) | ^!(y%%400)" rem 上月有!d!天 set /a "d=f*(28+leap)+s*30+(^!f&^!s)*31" set /a "y1=y-^!(m-12)" set "m=0!m!" set "m=!m:~-2!" set "d=0!d!" set "d=!d:~-2!" set DstDate=!y1!-!m!-!d! echo,%DstDate% pause 【方案四】BAT + Ritchie Lawrence 日期函数 复制代码 代码如下: @echo off rem 假设系统日期格式为yyyy-mm-dd set ThisYear=%date:~0,4% set ThisMonth=%date:~5,2% call :DateToDays %ThisYear% %ThisMonth% 01 ThisDays set /a DstDays=%ThisDays%-1 call :DaysToDate %DstDays% DstYear DstMonth DstDay set DstDate=%DstYear%-%DstMonth%-%DstDay% echo,%DstDate% pause goto :eof :DateToDays %yy% %mm% %dd% days :DaysToDate %days% yy mm dd |
请发表评论