如何在批处理命令中将12小时时间转换为24小时

时间:2014-01-22 03:03:43

标签: batch-file batch-processing batch-rename

我是批处理命令的新手,最近我正在处理批处理脚本以获取文件更新时间。

:GET_FILE_UPDATE_TIME

rem ****************************************
rem ** Get the modify time of the file
rem ****************************************
FOR %%f IN (%~1) DO SET filedatetime=%%~tf

echo %~1 update time :%filedatetime%>>%OUT_DIR%\%CORREL_LOG_UPDATE_TIME%.txt

rem Get the file update time
set filehour=%filedatetime:~11,2%

REM below is to remove the 0 from the time , eg. 09AM change into 9AM 
SET /a filehour=1!filehour!-(11!filehour!-1!filehour!)/10
set filemins=%filedatetime:~14,2%
SET /a filemins=1!filemins!-(11!filemins!-1!filemins!)/10
set /A filetime=(%filehour%*60*60)+(%filemins%*60)

goto:eof

起初我认为脚本运行正常,但后来我发现它有一个错误。 有些文件正在返回12小时的时间格式,例如关注。

  

提交时间= 01/20/20 2014 07:12 PM

或以下(对于AM)

  

SET filedatetime = 21/01/01 2014 07:52

  

SET filedatetime = 01/21/2014 05:08 AM

在进行计算时,它变成了7:12 AM,而实际上是19:12 PM。

有人可以帮忙解决这个问题吗?

3 个答案:

答案 0 :(得分:1)

EDITED - 日期输出中的本地化通常会在需要解析时导致噩梦。添加了代码以尝试清理日期和时间。不,没有防弹,我无法访问所有可能的本地化来测试它。可能需要进行一些调整。

@echo off

    setlocal enableextensions

    for %%f in ("%~f0") do call :get24HDateTime "%%~tf" dt 
    echo %dt%

    call :get24HDateTime "%date% 01:00 AM"
    call :get24HDateTime "%date% 01:00 PM"
    call :get24HDateTime "%date% 00:00 AM"
    call :get24HDateTime "%date% 23:00"
    call :get24HDateTime "%date% 07:00"

    endlocal

exit /b

:get24HDateTime "inputDateTime" outputVariable
    setlocal enableextensions enabledelayedexpansion
    set "input=%~1"
    set "DT="
    for /f "tokens=*" %%a in ('cmd /d /q /e:on /c "for %%f in ("%input: =" "%") do echo(%%~f"^|findstr /r /b /e /c:"[0-9/.-]*" /c:"[0-9][0-9]:[0-9][0-9]" /c:"[AP]M"') do (
        set "DT=!DT! %%~a"
    )
    for /f "tokens=1-6 delims=A/: " %%a in ("%DT:PM= PM%") do (
        if not "%%f"=="PM" ( set "H=%%d" ) else ( set /a "H=(1%%d %% 100)+12" )
        set "DT=%%a/%%b/%%c !H!:%%e"
    )
    endlocal & if not "%~2"=="" ( set "%~2=%DT%" ) else echo(%DT%
    exit /b

答案 1 :(得分:1)

@ECHO OFF
SETLOCAL
FOR %%x IN ("01/20/2014 07:12" "01/20/2014 3:22" "01/20/2014 17:12" "21/01/2014 07:52" "01/21/2014 05:08") DO (
 SET "filedatetime=%%~x"
 CALL :gfutq
 CALL ECHO produces %%filedatetime%% and filetime=%%filetime%% and filetimez=%%filetimez%%
 SET "filedatetime=%%~x AM"
 CALL :gfutq
 CALL ECHO produces %%filedatetime%% and filetime=%%filetime%% and filetimez=%%filetimez%%
 SET "filedatetime=%%~x PM"
 CALL :gfutq
 CALL ECHO produces %%filedatetime%% and filetime=%%filetime%% and filetimez=%%filetimez%%
)
GOTO :EOF
:GET_FILE_UPDATE_TIME

rem ****************************************
rem ** Get the modify time of the file
rem ****************************************
FOR %%f IN (%~1) DO SET filedatetime=%%~tf

echo %~1 update time :%filedatetime%>>%OUT_DIR%\%CORREL_LOG_UPDATE_TIME%.txt

:gfutq

ECHO original filedatetime=%filedatetime%

SET "filetimez="
SET "f1224="
:: first - have we got "AM" or "PM" as last 2 characters?
IF "%filedatetime:~-2%"=="AM" SET /a f1224=0
IF "%filedatetime:~-2%"=="PM" SET /a f1224=12
IF DEFINED F1224 SET "filedatetime=%filedatetime:~0,-3%"
:: last 2 characters=minutes, make decimal
set /a filemins=1%filedatetime:~-2% -100
SET "filedatetime=%filedatetime:~0,-3%"
:: last 2 characters=hour
set "filehour=%filedatetime:~-2%"
SET "filedatetime=%filedatetime:~0,-2%"
SET "filedatetime=%filedatetime: =%"
:: ensure leading spaces are made 0 (in case of leading-zero suppression)
:: prefix the result with 1 and subtract 100 giving decimal hours
set /a filehour=1%filehour: =0%-100
:: no suffix found?
IF NOT DEFINED F1224 GOTO gfutns
:: 12-hour clock in use; if 12:?? make 0:?? then add 0 or 12
IF %filehour% gtr 12 SET filetime=makes no sense&GOTO :EOF
IF %filehour%==12 (SET /a filehour=%f1224%) ELSE (SET /a filehour+=%f1224%)
IF %filehour% gtr 24 SET filetime=makes no sense&GOTO :EOF

:gfutns
set /A filetime=(%filehour%*60*60)+(%filemins%*60)
SET filemins=0%filemins%
SET filehour=0%filehour%
SET filetimez=%filehour:~-2%%filemins:~-2%

goto:eof

这是一个应该处理所有情况的小演示。

我已经任意定义了第13次:??如果它们跟在AMPM之后没有任何意义。

返回的数字是从午夜计算的秒数和零填充的hhmm。返回filedatetime并删除时间元素。

请注意,我正在使用不同的例程条目,因为我已经从测试值设置了所需的日期时间字符串。

答案 2 :(得分:0)

通过更改变量名称等,您应该能够非常轻松地将其合并到您的代码中。

set time=07:12 PM

if "%time%"=="%time:AM=%" set /a t=%time:~0,2%+12

if defined t set time=%t%%time:~2%

echo %time%

根据约定00:00 AM / PM给出一个变量,这将检查时间不是 AM,如果是,它将转换为24小时时间。