- Instant help with your JavaScript coding problems

# Convert milliseconds to minutes and seconds

Question:
How to convert milliseconds to minutes and seconds?
``````function convertMsToTime(ms) {
const totalSeconds = Math.round(ms / 1000);
const hours = Math.floor(totalSeconds / 3600);
const minutes = Math.floor((totalSeconds - hours * 3600) / 60);
const seconds = totalSeconds - hours * 3600 - minutes * 60;

return `\${hours}:\${minutes}:\${seconds}`;
}``````
Description:

To convert milliseconds to hours, minutes, and seconds you need to execute some mathematical calculations.

• Convert the milliseconds value to seconds by dividing it by 1000 and converting it to an integer using the `Math.round()` method.
• Then calculate how many whole hours the resulting seconds add up to. Here you have to use the `Math.floor()` method.
• Then see how many whole minutes the remaining seconds add up to.
• Finally, subtracting the seconds in whole hours and whole minutes from the original `totalSeconds` value gives the remaining seconds.
Share "How to convert milliseconds to minutes and seconds?"
Tags:
convert, millisecond, hour, minute, second, javascript
Technical term:
Convert milliseconds to minutes and seconds