How to Convert Milliseconds to Readable Time Format in Batch Script
Many system tools, performance counters, and APIs report durations in Milliseconds (ms). While 3661500 ms is precise, it is far more useful to display it as "1 Hour, 1 Minute, 1 Second, 500 ms." This conversion requires a cascade of divisions and modulo operations to extract each time component from smallest to largest.
In this guide, we will demonstrate how to break down a raw millisecond value into a formatted time string.
The Strategy: The Four-Tier Cascade
- Total Seconds: Divide milliseconds by 1000.
- Remaining ms: Modulo 1000.
- Minutes: Divide total seconds by 60.
- Hours: Divide total minutes by 60.
Implementation Script
@echo off
setlocal enabledelayedexpansion
:: 1. Input: Raw milliseconds
set /p "ms=Enter total milliseconds: "
:: 2. Cascade Extraction
:: Get remaining ms
set /a "remainMs=ms %% 1000"
:: Get total seconds
set /a "totalSec=ms / 1000"
:: Get seconds remainder
set /a "sec=totalSec %% 60"
:: Get total minutes
set /a "totalMin=totalSec / 60"
:: Get minutes remainder
set /a "min=totalMin %% 60"
:: Get hours
set /a "hr=totalMin / 60"
:: 3. Zero-Pad for Display
if !hr! LSS 10 set "hr=0!hr!"
if !min! LSS 10 set "min=0!min!"
if !sec! LSS 10 set "sec=0!sec!"
:: 4. Zero-Pad milliseconds to 3 digits
if !remainMs! LSS 10 (
set "remainMs=00!remainMs!"
) else if !remainMs! LSS 100 (
set "remainMs=0!remainMs!"
)
echo.
echo ==========================================
echo RAW INPUT: !ms! ms
echo FORMATTED: !hr!:!min!:!sec!.!remainMs!
echo ==========================================
pause
The remaining milliseconds value (remainMs) can be 1, 2, or 3 digits. Without padding, 5 ms would display as .5 instead of .005, and 50 ms would display as .50 instead of .050. The 3-digit padding ensures the fractional part is always unambiguous and consistent with the HH:MM:SS.mmm standard.
Why Convert Milliseconds?
- Performance Benchmarking: Reporting script execution time as "00:02:35.120" instead of "155120 ms" is far more professional and readable.
- API Response Times: Logging how long a web request took in a human-friendly format for health dashboards.
- Media Processing: Tools like FFmpeg report encoding time in milliseconds. Converting this to HH:MM:SS helps estimate remaining time for large batch encoding jobs.
Important Considerations
Batch integers max out at 2,147,483,647. This means you can handle up to about 24.8 days worth of milliseconds. For longer durations, use PowerShell or split the value before processing.
If your millisecond count exceeds 86,400,000 (24 hours), add a "Days" tier at the top of the cascade after computing hours:
set /a "days=hr / 24"
set /a "hr=hr %% 24"
Then prepend !days!d to the formatted output string.
Best Practices
- Start/End Timer: Use this in conjunction with a timer: capture
%TIME%at the start and end of a block, convert both to ms, subtract, and then format the result. - Log-Friendly Output: The
HH:MM:SS.mmmformat is a widely accepted standard for log files and makes parsing with external tools much easier.
Conclusion
Converting milliseconds to a readable time format is a vital "polishing" step for any performance-monitoring script. By breaking down raw numbers into hours, minutes, seconds, and milliseconds, you transform cryptic system data into clear, professional output. This ability to present time data in a human-friendly format is essential for building high-quality dashboards, benchmarking reports, and execution logs.