How to Convert a 12-Hour Time to 24-Hour Format in Batch Script
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
- Extract the hour and the AM/PM indicator.
- If PM and the hour is not 12, add 12 to the hour.
- 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
The 12-hour to 24-hour mapping follows two special rules:
- 12:00 AM (midnight) becomes 00:00: the hour resets to zero.
- 12:00 PM (noon) stays 12:00: no addition needed.
All other PM hours have 12 added (e.g., 1 PM → 13, 11 PM → 23), and all other AM hours remain unchanged (e.g., 9 AM → 09).
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?
- Sortability: Files named
Report_14-30.txtsort correctly in chronological order, whileReport_02-30-PM.txtdoes not. - 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.
- International Standards: The 24-hour format is the international standard (ISO 8601) and is expected by most databases and logging systems.
Important Considerations
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).
If your input includes seconds (e.g., 02:30:45 PM), add a third token to the time parsing loop:
for /f "tokens=1,2,3 delims=:" %%A in ("!timePart!") do (
set /a "hh=1%%A - 100"
set "mm=%%B"
set "ss=%%C"
)
Then include !ss! in the output: !hh!:!mm!:!ss!.
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.