Board logo

标题: [日期时间] [分享]批处理转换文本中的日期时间格式 [打印本页]

作者: Batcher    时间: 2019-10-29 11:49     标题: [分享]批处理转换文本中的日期时间格式

问题:
有效期:Fri Oct 24 16:08:06 GMT+08:00 2018 至Sun Oct 25 16:08:07 GMT+08:00 2020
有效期:Fri Nov 26 16:08:08 GMT+08:00 2018 至Sun Nov 27 16:08:09 GMT+08:00 2020

希望把格式转换成如下

有效期:2018-10-24 16:08:06 至 2020-10-25 16:08:07
有效期:2018-11-26 16:08:08 至 2020-11-27 16:08:09

代码:
  1. @echo off
  2. setlocal enabledelayedexpansion
  3. (for /f "delims=" %%i in ('type "1.txt"') do (
  4.     set "str=%%i"
  5.     set yearFrom=!str:~34,4!
  6.     set monthFrom=!str:~8,3!
  7.     set dayFrom=!str:~12,2!
  8.     set timeFrom=!str:~15,8!
  9.     set yearTo=!str:~70,4!
  10.     set monthTo=!str:~44,3!
  11.     set dayTo=!str:~48,2!
  12.     set timeTo=!str:~51,8!
  13.     call :GetMonth !monthFrom!
  14.     call :GetMonth !monthTo!
  15.     set tsFrom=!yearFrom!-!monthNum!-!dayFrom! !timeFrom!
  16.     set tsTo=!yearTo!-!monthNum!-!dayTo! !timeTo!
  17.     echo 有效期:!tsFrom! 至 !tsTo!
  18. ))>"2.txt"
  19. goto :eof
  20. :GetMonth
  21. if "%1" equ "Jan" (
  22.     set "monthNum=01"
  23. ) else if "%1" equ "Feb" (
  24.     set "monthNum=02"
  25. ) else if "%1" equ "Mar" (
  26.     set "monthNum=03"
  27. ) else if "%1" equ "Apr" (
  28.     set "monthNum=04"
  29. ) else if "%1" equ "May" (
  30.     set "monthNum=05"
  31. ) else if "%1" equ "Jun" (
  32.     set "monthNum=06"
  33. ) else if "%1" equ "Jul" (
  34.     set "monthNum=07"
  35. ) else if "%1" equ "Aug" (
  36.     set "monthNum=08"
  37. ) else if "%1" equ "Sep" (
  38.     set "monthNum=09"
  39. ) else if "%1" equ "Oct" (
  40.     set "monthNum=10"
  41. ) else if "%1" equ "Nov" (
  42.     set "monthNum=11"
  43. ) else if "%1" equ "Dec" (
  44.     set "monthNum=12"
  45. ) else (
  46.     set "monthNum=ERROR"
  47. )
  48. goto :eof
复制代码





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