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 ==========================================
pauseInteractive 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 ==========================================
pauseWhy 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
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.