无忧启动论坛

标题: bat小玩具 简单计算两个日期相差的天数 [打印本页]

作者: sratlf    时间: 2012-5-18 14:37
标题: bat小玩具 简单计算两个日期相差的天数
真的只是很简单的计算  没考虑闰年 闰月 闰秒之类的  

需要指定两个参数  第一日期和第二日期  格式都是 yyyy-mm-dd   执行结束后变量 time 值就是两个日期相差的天数

不想复制的可以直接下载附件使用    TIME.zip (742 Bytes, 下载次数: 76)

!BAT  time by sratlf 05-18
::usage: %~0 yyyy-mm-dd yyyy-mm-dd

setlocal
debug 0
set t1=%~1
set t2=%~2

::起始日期
set /a y1=%t1:~,4%
set /a m1=%t1:~5,2%
set /a d1=%t1:~-2%
::终止日期
set /a y2=%t2:~,4%
set /a m2=%t2:~5,2%
set /a d2=%t2:~-2%

::值判断
if %m1%<=12 if %m2%<=12 goto :m%m1% || exit
:m1
:m3
:m5
:m7
:m8
:m10
:m12
if %d1%<=31 goto :m_%m2% || exit
:m2
if %d1%<=29 goto :m_%m2% || exit
:m4
:m6
:m9
:m11
if %d1%<=30 goto :m_%m2% || exit

:m_1
:m_3
:m_5
:m_7
:m_8
:m_10
:m_12
if %d2%<=31 goto :start || exit
:m_2
if %d2%<=29 goto :start || exit
:m_4
:m_6
:m_9
:m_11
if %d2%<=30 goto :start || exit

:start
::判断
set time=0
if %y1%>=%y2% || goto :end_time
if %y1%<=%y2% || goto :exchange
if %m1%>=%m2% || goto :end_time
if %m1%<=%m2% || goto :exchange
if %d1%>=%d2% || goto :end_time
if %d1%<=%d2% || goto :exchange
endlocal && set time=0
exit

:exchange
set y1=%y2% && set y2=%y1%
set m1=%m2% && set m2=%m1%
set d1=%d2% && set d2=%d1%

:end_time
if %m2%>=2 set /a time=%time% + 31 || set /a time=%time% + %d2% && goto :end_time_y
if %m2%>=3 set /a time=%time% + 28 || set /a time=%time% + %d2% && goto :end_time_y
if %m2%>=4 set /a time=%time% + 31 || set /a time=%time% + %d2% && goto :end_time_y
if %m2%>=5 set /a time=%time% + 30 || set /a time=%time% + %d2% && goto :end_time_y
if %m2%>=6 set /a time=%time% + 31 || set /a time=%time% + %d2% && goto :end_time_y
if %m2%>=7 set /a time=%time% + 30 || set /a time=%time% + %d2% && goto :end_time_y
if %m2%>=8 set /a time=%time% + 31 || set /a time=%time% + %d2% && goto :end_time_y
if %m2%>=9 set /a time=%time% + 31 || set /a time=%time% + %d2% && goto :end_time_y
if %m2%>=10 set /a time=%time% + 30 || set /a time=%time% + %d2% && goto :end_time_y
if %m2%>=11 set /a time=%time% + 31 || set /a time=%time% + %d2% && goto :end_time_y
if %m2%>=12 set /a time=%time% + 30 || set /a time=%time% + %d2% && goto :end_time_y
set /a time=%time% + %d2%

:end_time_y
calc %y2% - %y1%
calc %@retval% * 365
set /a time=%time% + %@retval%

:start_time
if %m1%>=2 set /a time=%time% - 31 || set /a time=%time% - %d1% && goto :end
if %m1%>=3 set /a time=%time% - 28 || set /a time=%time% - %d1% && goto :end
if %m1%>=4 set /a time=%time% - 31 || set /a time=%time% - %d1% && goto :end
if %m1%>=5 set /a time=%time% - 30 || set /a time=%time% - %d1% && goto :end
if %m1%>=6 set /a time=%time% - 31 || set /a time=%time% - %d1% && goto :end
if %m1%>=7 set /a time=%time% - 30 || set /a time=%time% - %d1% && goto :end
if %m1%>=8 set /a time=%time% - 31 || set /a time=%time% - %d1% && goto :end
if %m1%>=9 set /a time=%time% - 31 || set /a time=%time% - %d1% && goto :end
if %m1%>=10 set /a time=%time% - 30 || set /a time=%time% - %d1% && goto :end
if %m1%>=11 set /a time=%time% - 31 || set /a time=%time% - %d1% && goto :end
if %m1%>=12 set /a time=%time% - 30 || set /a time=%time% - %d1% && goto :end
set /a time=%time% - %d1%

:end
endlocal && set /a time=%time% - 1 > nul
exit


附使用截图一张



[ 本帖最后由 sratlf 于 2012-5-18 14:38 编辑 ]
作者: xiaoy    时间: 2012-5-18 14:47
太感谢了,解决了我的难题 辛苦了
作者: 135956    时间: 2012-5-19 15:23
你的小工具很多啊。。。很NB。




欢迎光临 无忧启动论坛 (http://bbs.wuyou.net/) Powered by Discuz! X3.3