无忧启动论坛

 找回密码
 注册
搜索
系统gho:最纯净好用系统下载站投放广告、加入VIP会员,请联系 微信:wuyouceo
查看: 3172|回复: 2
打印 上一主题 下一主题

[分享] bat小玩具 简单计算两个日期相差的天数

[复制链接]
跳转到指定楼层
1#
发表于 2012-5-18 14:37:14 | 只看该作者 回帖奖励 |倒序浏览 |阅读模式
真的只是很简单的计算  没考虑闰年 闰月 闰秒之类的  

需要指定两个参数  第一日期和第二日期  格式都是 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 编辑 ]
2#
发表于 2012-5-18 14:47:56 | 只看该作者
太感谢了,解决了我的难题 辛苦了
回复

使用道具 举报

3#
发表于 2012-5-19 15:23:56 | 只看该作者
你的小工具很多啊。。。很NB。
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|捐助支持|无忧启动 ( 闽ICP备05002490号-1 )

闽公网安备 35020302032614号

GMT+8, 2024-11-17 04:35

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表