Board logo

标题: [日期时间] 批处理计算指定秒数之后的日期时间 [打印本页]

作者: HAT    时间: 2011-10-17 22:46     标题: 批处理计算指定秒数之后的日期时间

【方案一】
  1. @echo off
  2. set OffsetSeconds=30
  3. set PSCMD=powershell -c "Get-Date (Get-Date).AddSeconds(%OffsetSeconds%) -uformat ""%%Y-%%m-%%d %%H:%%M:%%S"""
  4. for /f "tokens=1-2" %%i in ('%PSCMD%') do (
  5.     set "DstDate=%%i"
  6.     set "DstTime=%%j"
  7. )
  8. echo %date% %time%
  9. echo,%DstDate% %DstTime%
  10. pause
复制代码
【方案二】
  1. @echo off
  2. rem 指定开始时间
  3. set date=2011-10-17
  4. set TimeBegin=%date:~0,10% %time:~0,8%
  5. rem 指定间隔的秒数
  6. set SecondInter=3
  7. echo,%TimeBegin%
  8. call :DateToSecs %TimeBegin:~0,4% %TimeBegin:~5,2% %TimeBegin:~8,2% %TimeBegin:~11,2% %TimeBegin:~14,2% %TimeBegin:~17,2% SecondsBegin
  9. set /a SecondsEnd=SecondsBegin+SecondInter
  10. call :SecsToDate %SecondsEnd% YearEnd MonthEnd DayEnd HourEnd MinuteEnd SecondEnd
  11. set TimeEnd=%YearEnd%-%MonthEnd%-%DayEnd% %HourEnd%:%MinuteEnd%:%SecondEnd%
  12. echo,%TimeEnd%
  13. pause
  14. goto :eof
  15. :DateToSecs %yy% %mm% %dd% %hh% %nn% %ss% secs
  16. setlocal ENABLEEXTENSIONS
  17. set yy=%1&set mm=%2&set dd=%3&set hh=%4&set nn=%5&set ss=%6
  18. if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
  19. set /a dd=100%dd%%%100,mm=100%mm%%%100
  20. set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
  21. set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
  22. if 1%hh% LSS 20 set hh=0%hh%
  23. if {%nn:~2,1%} EQU {p} if "%hh%" NEQ "12" set hh=1%hh%&set/a hh-=88
  24. if {%nn:~2,1%} EQU {a} if "%hh%" EQU "12" set hh=00
  25. if {%nn:~2,1%} GEQ {a} set nn=%nn:~0,2%
  26. set /a hh=100%hh%%%100,nn=100%nn%%%100,ss=100%ss%%%100
  27. set /a j=j*86400+hh*3600+nn*60+ss
  28. endlocal&set %7=%j%&goto :EOF
  29. :SecsToDate %secs% yy mm dd hh nn ss
  30. setlocal ENABLEEXTENSIONS
  31. set /a i=%1,ss=i%%60,i/=60,nn=i%%60,i/=60,hh=i%%24,dd=i/24,i/=24
  32. set /a a=i+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
  33. set /a d=4*c+3,d/=1461,e=-1461*d,e/=4,e+=c,m=5*e+2,m/=153,dd=153*m+2,dd/=5
  34. set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
  35. (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
  36. (if %hh% LSS 10 set hh=0%hh%)&(if %nn% LSS 10 set nn=0%nn%)
  37. if %ss% LSS 10 set ss=0%ss%
  38. endlocal&set %7=%ss%&set %6=%nn%&set %5=%hh%&^
  39. set %4=%dd%&set %3=%mm%&set %2=%yy%&goto :EOF
复制代码





欢迎光临 批处理之家 (http://www.bathome.net/) Powered by Discuz! 7.2