Skip to content

How to Convert a 12-Hour Time to 24-Hour Format in Batch Script

Learn how to convert 12-hour time (e.g., 02:30 PM) to 24-hour format (e.g., 14:30) in Windows Batch scripts using parsing logic for accurate file naming, sorting, and time comparisons.

Many system logs and user interfaces display time in the 12-Hour Format (e.g., 02:30 PM). However, for file naming, sorting, and mathematical comparison, the 24-Hour Format (e.g., 14:30) is far more practical. Converting between these formats requires parsing the hour, checking for the AM/PM suffix, and adjusting accordingly.

In this guide, we will demonstrate how to convert 12-hour time to 24-hour time in Batch.

The Strategy: The PM Offset

  1. Extract the hour and the AM/PM indicator.
  2. If PM and the hour is not 12, add 12 to the hour.
  3. If AM and the hour is 12, set it to 00 (midnight).

Implementation Script

@echo off
setlocal enabledelayedexpansion

:: 1. Input (Example: 02:30 PM)
set "input=02:30 PM"

echo Converting: !input!

:: 2. Parse the components
for /f "tokens=1,2 delims= " %%A in ("!input!") do (
    set "timePart=%%A"
    set "ampm=%%B"
)

for /f "tokens=1,2 delims=:" %%A in ("!timePart!") do (
    set /a "hh=1%%A - 100"
    set "mm=%%B"
)

:: 3. Apply Conversion Logic
if /i "!ampm!"=="PM" (
    if !hh! NEQ 12 set /a "hh+=12"
) else (
    if !hh! EQU 12 set /a "hh=0"
)

:: 4. Zero-pad the hour
if !hh! LSS 10 set "hh=0!hh!"

echo.
echo ==========================================
echo 12-HOUR: !input!
echo 24-HOUR: !hh!:!mm!
echo ==========================================
pause

Interactive Version

To accept user input instead of a hardcoded value:

@echo off
setlocal enabledelayedexpansion

:: Prompt for input
set /p "input=Enter time (e.g., 02:30 PM): "

:: Parse the components
for /f "tokens=1,2 delims= " %%A in ("!input!") do (
    set "timePart=%%A"
    set "ampm=%%B"
)

:: Validate that AM/PM was provided
if "!ampm!"=="" (
    echo [ERROR] Invalid format. Use "HH:MM AM" or "HH:MM PM".
    pause
    exit /b
)

for /f "tokens=1,2 delims=:" %%A in ("!timePart!") do (
    set /a "hh=1%%A - 100"
    set "mm=%%B"
)

:: Validate hour range
if !hh! LSS 1 if !hh! NEQ 0 (
    echo [ERROR] Invalid hour.
    pause
    exit /b
)
if !hh! GTR 12 (
    echo [ERROR] Hour must be between 1 and 12 for 12-hour format.
    pause
    exit /b
)

:: Apply Conversion Logic
if /i "!ampm!"=="PM" (
    if !hh! NEQ 12 set /a "hh+=12"
) else if /i "!ampm!"=="AM" (
    if !hh! EQU 12 set /a "hh=0"
) else (
    echo [ERROR] Invalid AM/PM indicator: !ampm!
    pause
    exit /b
)

:: Zero-pad the hour
if !hh! LSS 10 set "hh=0!hh!"

echo.
echo ==========================================
echo 12-HOUR: !input!
echo 24-HOUR: !hh!:!mm!
echo ==========================================
pause

Why Convert to 24-Hour Format?

  1. Sortability: Files named Report_14-30.txt sort correctly in chronological order, while Report_02-30-PM.txt does not.
  2. Math Compatibility: Comparing "Is 14:30 after 09:00?" is a simple integer comparison. Comparing "Is 2:30 PM after 9:00 AM?" requires additional parsing logic.
  3. International Standards: The 24-hour format is the international standard (ISO 8601) and is expected by most databases and logging systems.

Important Considerations

Conclusion

Converting 12-hour time to 24-hour format is a crucial "normalization" step for any script that handles timestamps. By standardizing all times into the sortable, comparable 24-hour format, you eliminate ambiguity and simplify all subsequent time-based logic. This conversion is a foundational tool for building professional logging, scheduling, and reporting systems.

Navigation