标题: [日期时间] [分享]批处理转换文本中的日期时间格式 [打印本页]
作者: 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
代码:- @echo off
- setlocal enabledelayedexpansion
- (for /f "delims=" %%i in ('type "1.txt"') do (
- set "str=%%i"
- set yearFrom=!str:~34,4!
- set monthFrom=!str:~8,3!
- set dayFrom=!str:~12,2!
- set timeFrom=!str:~15,8!
- set yearTo=!str:~70,4!
- set monthTo=!str:~44,3!
- set dayTo=!str:~48,2!
- set timeTo=!str:~51,8!
- call :GetMonth !monthFrom!
- call :GetMonth !monthTo!
- set tsFrom=!yearFrom!-!monthNum!-!dayFrom! !timeFrom!
- set tsTo=!yearTo!-!monthNum!-!dayTo! !timeTo!
- echo 有效期:!tsFrom! 至 !tsTo!
- ))>"2.txt"
- goto :eof
-
- :GetMonth
- if "%1" equ "Jan" (
- set "monthNum=01"
- ) else if "%1" equ "Feb" (
- set "monthNum=02"
- ) else if "%1" equ "Mar" (
- set "monthNum=03"
- ) else if "%1" equ "Apr" (
- set "monthNum=04"
- ) else if "%1" equ "May" (
- set "monthNum=05"
- ) else if "%1" equ "Jun" (
- set "monthNum=06"
- ) else if "%1" equ "Jul" (
- set "monthNum=07"
- ) else if "%1" equ "Aug" (
- set "monthNum=08"
- ) else if "%1" equ "Sep" (
- set "monthNum=09"
- ) else if "%1" equ "Oct" (
- set "monthNum=10"
- ) else if "%1" equ "Nov" (
- set "monthNum=11"
- ) else if "%1" equ "Dec" (
- set "monthNum=12"
- ) else (
- set "monthNum=ERROR"
- )
- goto :eof
复制代码
欢迎光临 批处理之家 (http://www.bathome.net/) |
Powered by Discuz! 7.2 |