Skip to main content

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

While the 24-hour format is ideal for sorting and computation, many end-user reports and display screens require the friendlier 12-Hour Format with an AM/PM suffix. Converting 14:30 to 2:30 PM requires checking if the hour is above 12 and adjusting it while appending the correct suffix.

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

The Strategy: The Midday Split

  1. Extract the hour.
  2. If the hour is 0, it's 12 AM (midnight).
  3. If the hour is between 1 and 11, it's AM.
  4. If the hour is 12, it's 12 PM (noon).
  5. If the hour is between 13 and 23, subtract 12 and label it PM.

Implementation Script

@echo off
setlocal enabledelayedexpansion

:: 1. Input (Example: 14:30 or 08:15)
set /p "input=Enter time in 24-hour format (HH:MM): "

:: 2. Parse and basic validation
set "h_raw=" & set "m_raw="
for /f "tokens=1,2 delims=:" %%A in ("!input!") do (
set "h_raw=%%A"
set "m_raw=%%B"
)

:: Check if both tokens exist
if "!h_raw!"=="" (
echo [ERROR] Invalid format. Please use HH:MM.
pause & exit /b
)
if "!m_raw!"=="" (
echo [ERROR] Invalid format. Please use HH:MM.
pause & exit /b
)

:: Safely convert to integers (handling leading zeros/octal trap)
:: If it starts with 0 and isn't just 0, we strip it to avoid octal interpretation
set "h_val=!h_raw!"
if "!h_val:~0,1!"=="0" if "!h_val!" neq "0" set "h_val=!h_val:~1!"
set /a "hh=h_val" 2>nul

set "m_val=!m_raw!"
if "!m_val:~0,1!"=="0" if "!m_val!" neq "0" set "m_val=!m_val:~1!"
set /a "mm=m_val" 2>nul

:: 3. Validate hour and minute range
set "valid=1"
if !hh! LSS 0 set "valid=0"
if !hh! GTR 23 set "valid=0"
if !mm! LSS 0 set "valid=0"
if !mm! GTR 59 set "valid=0"

if "!valid!"=="0" (
echo [ERROR] Invalid time: !h_raw!:!m_raw!. Use 00:00 to 23:59.
pause & exit /b
)

:: 4. Conversion Logic
set "suffix=AM"
set "display_hh=!hh!"

if !hh! EQU 0 (
set "display_hh=12"
) else if !hh! EQU 12 (
set "suffix=PM"
) else if !hh! GTR 12 (
set /a "display_hh=!hh!-12"
set "suffix=PM"
)

:: 5. Final Formatting (Zero-padding)
if !display_hh! LSS 10 set "display_hh=0!display_hh!"
if !mm! LSS 10 (set "display_mm=0!mm!") else (set "display_mm=!mm!")

echo.
echo ==========================================
echo 24-HOUR: !input!
echo 12-HOUR: !display_hh!:!display_mm! !suffix!
echo ==========================================
pause
Conversion Rules

The 24-hour to 12-hour mapping follows two special rules:

  • 00:00 (midnight) becomes 12:00 AM: the hour wraps to 12, not 0.
  • 12:00 (noon) stays 12:00 PM: no subtraction needed.

All other hours 13–23 have 12 subtracted (e.g., 14 → 2 PM, 23 → 11 PM), and hours 1–11 remain unchanged with an AM suffix.

Why Convert to 12-Hour Format?

  1. User-Facing Reports: When generating a summary for non-technical users, 2:30 PM is much more intuitive than 14:30.
  2. Notification Messages: If your script sends an email alert saying "Backup completed at 3:45 PM," it feels more natural than "15:45."
  3. Legacy Systems: Some older applications and hardware clocks only accept 12-hour time inputs.

Important Considerations

Leading Zero Octal Trap

Batch treats numbers with leading zeros as octal. The hour values 08 and 09 are invalid octal numbers and cause errors with set /a. The trick set /a "hh=1%%A - 100" prepends a 1 to create a valid decimal number (e.g., 108), then subtracts 100 to get the correct value (8).

Leading Zero Style

Decide early in your script whether to use 2:30 PM (no leading zero) or 02:30 PM (with leading zero) and apply it consistently. To omit the leading zero, remove the zero-padding step. The script above pads by default for consistent column alignment in reports and logs.

Seconds Support

If your input includes seconds (e.g., 14:30:45), add a third token to the parsing loop:

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

Then include !ss! in the output: !hh!:!mm!:!ss! !suffix!.

Conclusion

Converting 24-hour time to 12-hour format is the complementary skill to its reverse conversion. Together, they give your scripts full flexibility in how time is stored, compared, and displayed. By mastering both directions, you ensure that your automation tools can communicate with both system-level precision and user-friendly clarity.