[新手上路]批处理新手入门导读[视频教程]批处理基础视频教程[视频教程]VBS基础视频教程[批处理精品]批处理版照片整理器
[批处理精品]纯批处理备份&还原驱动[批处理精品]CMD命令50条不能说的秘密[在线下载]第三方命令行工具[在线帮助]VBScript / JScript 在线参考
返回列表 发帖

[日期时间] [已解决]批处理跨年份日期问题?

本帖最后由 cbnuisve 于 2015-8-11 10:38 编辑

在跨年份的时间得出的结果会出错。
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. set DaysAgo=7
  4. call :DateToDays %date:~0,4% %date:~5,2% %date:~8,2% PassDays
  5. set /a PassDays-=%DaysAgo%
  6. call :DaysToDate %PassDays% DstYear DstMonth DstDay
  7. set "yyyy=%DATE:~0,4%"
  8. echo %yyyy%%DstMonth%%DstDay%\
  9. :DateToDays %yy% %mm% %dd% days
  10. setlocal ENABLEEXTENSIONS
  11. set yy=%1&set mm=%2&set dd=%3
  12. if 1%yy% LSS 200 if 1%yy% LSS 170 (set yy=20%yy%) else (set yy=19%yy%)
  13. set /a dd=100%dd%%%100,mm=100%mm%%%100
  14. set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
  15. set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
  16. endlocal&set %4=%j%&goto :EOF
  17. :DaysToDate %days% yy mm dd
  18. setlocal ENABLEEXTENSIONS
  19. set /a a=%1+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
  20. 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
  21. set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
  22. (if %mm% LSS 10 set mm=0%mm%)&(if %dd% LSS 10 set dd=0%dd%)
  23. endlocal&set %2=%yy%&set %3=%mm%&set %4=%dd%&goto :EOF
复制代码

回复 2# Batcher


    假设把当前日期修改为2016年1月1日 ,计算出的当前日期七天前的日期是20161225 (echo %yyyy%%DstMonth%%DstDay%\)面不是20151225

TOP

本帖最后由 cbnuisve 于 2015-8-11 09:46 编辑

回复 4# flyinnet9
非常感谢你。你这批处理解决了我之前哪跨年的问题,但是又有个新问题,当日期是2016年3月1日时,获取前七天的日期会是20160251而不是正确的 20160223

TOP

回复 5# DAIC
  1. C:\Users\user\Desktop>setlocal enabledelayedexpansion
  2. C:\Users\user\Desktop>set DaysAgo=7
  3. C:\Users\user\Desktop>call :DateToDays 2016 01 01 PassDays
  4. C:\Users\user\Desktop>setlocal ENABLEEXTENSIONS
  5. C:\Users\user\Desktop>set yy=2016  & set mm=01  & set dd=01
  6. C:\Users\user\Desktop>if 12016 LSS 200 if 12016 LSS 170 (set yy=202016 )  else (set yy=192016 )
  7. C:\Users\user\Desktop>set /a dd=10001%100,mm=10001%100
  8. C:\Users\user\Desktop>set /a z=14-mm,z/=12,y=yy+4800-z,m=mm+12*z-3,j=153*m+2
  9. C:\Users\user\Desktop>set /a j=j/5+dd+y*365+y/4-y/100+y/400-2472633
  10. C:\Users\user\Desktop>endlocal & set PassDays=16801  & goto :EOF
  11. C:\Users\user\Desktop>set /a PassDays-=7
  12. C:\Users\user\Desktop>call :DaysToDate 16794 DstYear DstMonth DstDay
  13. C:\Users\user\Desktop>setlocal ENABLEEXTENSIONS
  14. C:\Users\user\Desktop>set /a a=16794+2472632,b=4*a+3,b/=146097,c=-b*146097,c/=4,c+=a
  15. C:\Users\user\Desktop>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
  16. C:\Users\user\Desktop>set /a dd=-dd+e+1,mm=-m/10,mm*=12,mm+=m+3,yy=b*100+d-4800+m/10
  17. C:\Users\user\Desktop>(if 12 LSS 10 set mm=012 )  & (if 25 LSS 10 set dd=025 )
  18. C:\Users\user\Desktop>endlocal & set DstYear=2015  & set DstMonth=12  & set DstDay=25  & goto :EOF
  19. C:\Users\user\Desktop>set "yyyy=2016"
  20. C:\Users\user\Desktop>echo 20161225\
  21. 20161225\
  22. C:\Users\user\Desktop>pause
复制代码

TOP

本帖最后由 cbnuisve 于 2015-8-11 10:11 编辑

回复 6# 回家路上
谢谢,看了好久还是没看明白。能否解释一下它是怎么运行的吗?如果我要获取七天前的日期要怎么做修改呢?
  1. @echo off & setlocal enabledelayedexpansion
  2. call :yesterday yesterday
  3. echo %yesterday%
  4. :yesterday
  5. for /f "tokens=2*" %%a in ('reg query "HKCU\Control Panel\International" /v sShortDate^|find/i"sSh"') do (
  6.   >nul reg add "HKCU\Control Panel\International" /v sShortDate /t REG_SZ /d "yyyyMMdd" /f
  7.   set /a "y=!date:~0,4!,m=1!date:~4,2!-100,d=1!date:~-2!-101"
  8.   setlocal disabledelayedexpansion
  9.   set /a "m-=!d,y-=!m,d+=!d*(31-(!(m-4)|!(m-6)|!(m-9)|!(m-11))-!(m-2)*(3-(!(y%%4)&!!(y%%100)|!(y%%400)))),m+=!m*12"
  10.   set /a "m+=100,d+=100"
  11.   >nul reg add "HKCU\Control Panel\International" /v sShortDate /t REG_SZ /d "%%~b" /f
  12. )
  13. endlocal & set "%1=%y%%m:~-2%%d:~-2%" & exit /b
复制代码

TOP

本帖最后由 cbnuisve 于 2015-8-11 10:42 编辑

回复 10# DAIC

谢谢,终于解决这问题了。

TOP

回复 12# 回家路上


    谢谢你,我的问题已经解决了。

TOP

返回列表