以下代码同样用 100 年的日期测试, 用时与 7 楼 FOR 的代码极为接近
对输入的日期前后无限制, 可自动将在前的日期作为起始日期
其中 如: if "!_y!!_m!%%d" gtr "!ymde!" (goto :break) 这样的判断语句还可省去, 进一步提高效率, 但未有简洁方案, 太显繁长- @echo off&setlocal enabledelayedexpansion
- set /p ymds=请输入起始日期(yyyymmdd),要使用系统当前日期,请直接回车:
- set /p ymde=请输入截止日期(yyyymmdd),要使用系统当前日期,请直接回车:
- (set t=%time%)&(cd.>outFile.txt)
- for /l %%m in (1,1,12) do (set days_%%m=31)
- for %%m in (4,6,9,11) do (set days_%%m=30)
- if not defined ymds (set ymds=%date:~0,4%%date:~5,2%%date:~8,2%)
- if not defined ymde (set ymde=%date:~0,4%%date:~5,2%%date:~8,2%)
- if "!ymde!" lss "!ymds!" (set tt=!ymde!)&(set ymde=!ymds!)&(set ymds=!tt!)
- echo 起始日期为:!ymds!, 截止日期为:!ymde!
- (set /a ys=1!ymds:~0,4!-10000, ye=1!ymde:~0,4!-10000)
- (set /a ms=1!ymds:~4,2!-100, me=1!ymde:~4,2!-100)
- (set /a ds=1!ymds:~6,2!-100, de=1!ymde:~6,2!-100)
- set /a "y_1=ys+1,m_1=ms+1,d_1=ds+1"
- for /l %%y in (!ys!,1,!ys!) do (
- (set _y=000%%y)&(set _y=!_y:~-4!)
- for /l %%m in (!ms!,1,!ms!) do (
- (set _m=0%%m)&(set _m=!_m:~-2!)
- if %%m==2 set /a "days_2=28+(^!(%%y%%4)&^!^!(%%y%%100))|^!(%%y%%400)"
- for /l %%d in (!ds!,1,9) do (
- if "!_y!!_m!0%%d" gtr "!ymde!" (goto :break) else echo !_y!!_m!0%%d
- )
- if !ds! geq 10 (set d_2=!ds!) else set d_2=10
- for /l %%d in (!d_2!,1,!days_%%m!) do (
- if "!_y!!_m!%%d" gtr "!ymde!" (goto :break) else echo !_y!!_m!%%d
- )
- )
- for /l %%m in (!m_1!,1,12) do (
- (set _m=0%%m)&(set _m=!_m:~-2!)
- if %%m==2 set /a "days_2=28+(^!(%%y%%4)&^!^!(%%y%%100))|^!(%%y%%400)"
- for /l %%d in (1,1,9) do (
- if "!_y!!_m!0%%d" gtr "!ymde!" (goto :break) else echo !_y!!_m!0%%d
- )
- for /l %%d in (10,1,!days_%%m!) do (
- if "!_y!!_m!%%d" gtr "!ymde!" (goto :break) else echo !_y!!_m!%%d
- )
- )
- )>>outFile.txt
- for /l %%y in (!y_1!,1,!ye!) do (
- (set _y=000%%y)&(set _y=!_y:~-4!)
- for /l %%m in (1,1,12) do (
- (set _m=0%%m)&(set _m=!_m:~-2!)
- if %%m==2 set /a "days_2=28+(^!(%%y%%4)&^!^!(%%y%%100))|^!(%%y%%400)"
- for /l %%d in (1,1,9) do (
- if "!_y!!_m!0%%d" gtr "!ymde!" (goto :break) else echo !_y!!_m!0%%d
- )
- for /l %%d in (10,1,!days_%%m!) do (
- if "!_y!!_m!%%d" gtr "!ymde!" (goto :break) else echo !_y!!_m!%%d
- )
- )
- )>>outFile.txt
- :break
- echo 用时: !t! ==^> %time%
- start outFile.txt
- pause
- exit /b
复制代码
[ 本帖最后由 neorobin 于 2010-4-8 03:46 编辑 ] |