标题: [数值计算] 批处理怎样计算两个日期时间的差值? [打印本页]
作者: BatCoder 时间: 2011-2-19 19:46 标题: 批处理怎样计算两个日期时间的差值?
开始时间:2010-01-18 01:02:03
结束时间:2011-02-19 00:00:00
希望得到的结果:1年31日22时57分57秒
作者: Batcher 时间: 2011-2-19 20:11
- @echo off
- set StartTime=2010-01-18 01:02:03
- set EndTime=2011-02-19 00:00:00
- set StartTime=%StartTime:-= %
- set StartTime=%StartTime::= %
- set EndTime=%EndTime:-= %
- set EndTime=%EndTime::= %
- call :DateToSecs %StartTime% StartSecs
- call :DateToSecs %EndTime% EndSecs
- set /a DiffSecs=EndSecs-StartSecs
- set DiffSecs=%DiffSecs:-= %
- set /a Y=DiffSecs/31536000
- set /a D=(DiffSecs%%31536000)/86400
- set /a H=(DiffSecs%%86400)/3600
- set /a M=(DiffSecs%%3600)/60
- set /a S=DiffSecs%%60
- echo %Y%年%D%日%H%时%M%分%S%秒
- goto :eof
-
- :DateToSecs %yy% %mm% %dd% %hh% %nn% %ss% secs
- setlocal ENABLEEXTENSIONS
- set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5&set ss=%6
- if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
- set /a dd=100%dd%%%100,mm=100%mm%%%100
- set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
- set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
- if 1%hh% LSS 20 set hh=0%hh%
- if {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
- if {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
- if {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
- set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
- set /a j=j*86400+hh*3600+nn*60+ss
- endlocal&set %7=%j%&goto :EOF
复制代码
作者: pcl_test 时间: 2016-9-30 11:20
本帖最后由 pcl_test 于 2019-3-15 21:02 编辑
- @echo off
- set time1=2010-01-18 01:02:03
- set time2=2011-02-19 00:00:00
- >"%tmp%\$t.vbs" echo;WSH.echo DateDiff("s","%time1%","%time2%")
- for /f %%a in ('cscript -nologo "%tmp%\$t.vbs"') do set t=%%a
- set /a "y=t/31536000,d=(t %% 31536000)/86400,h=(t %% 86400)/3600,m=(t %% 3600)/60,s=t %% 60"
- echo;%y%年%d%天%h%时%m%分%s%秒
- pause
复制代码
欢迎光临 批处理之家 (http://bbs.bathome.net/) |
Powered by Discuz! 7.2 |