Convert Milliseconds To Hours, Minutes, Seconds In JavaScript

To convert milliseconds to hours, minutes and seconds in JavaScript –

  1. Divide the milliseconds by ‘1000’ to convert milliseconds to seconds.
  2. Divide the seconds by ’60’ to convert seconds to minutes.
  3. Divide the minutes by ’60’ to convert minutes to hours.
  4. You can use the modulus (%) operator to get the remainder from the division.
  5. Add the leading 0 to minutes and seconds if it’s less than 10.
convert milliseconds to hours, minutes, seconds in javascript

Convert Milliseconds To Hours, Minutes, Seconds In JavaScript

Let’s create a reusable function that converts milliseconds to hh:mm:ss format.

function convertMillisecondsToHumanReadableTime(ms) {

  let seconds = Math.floor(ms/1000);

  let minutes = Math.floor(seconds/60);

  let hours = Math.floor(minutes/60);

  seconds = seconds % 60;

  minutes = minutes % 60;

  hours = hours < 10 ? "0" + hours : hours;

  minutes = minutes < 10 ? "0" + minutes : minutes;

  seconds = seconds < 10 ? "0" + seconds : seconds;

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

}

convertMillisecondsToHumanReadableTime(5000000); //=> 01:23:20

convertMillisecondsToHumanReadableTime(15000); //=> 00:00:15

convertMillisecondsToHumanReadableTime(60000); //=> 00:01:00

You can do hours%24 if you want to reset to 0 after 24 hours.

Here is an example using the hours%24 to reset to 0 after 24 hours.

function convertMillisecondsToHumanReadableTime(ms) {

  let seconds = Math.floor(ms/1000);

  let minutes = Math.floor(seconds/60);

  let hours = Math.floor(minutes/60);

  hours = hours % 24;

  seconds = seconds % 60;

  minutes = minutes % 60;

  hours = hours < 10 ? "0" + hours : hours;

  minutes = minutes < 10 ? "0" + minutes : minutes;

  seconds = seconds < 10 ? "0" + seconds : seconds;

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

}

convertMillisecondsToHumanReadableTime(5000000000); //=> 20:53:20 (hours are reset to 0 after 24)

convertMillisecondsToHumanReadableTime(15000); //=> 00:00:15

convertMillisecondsToHumanReadableTime(60000); //=> 00:01:00

In the above code, we first converted milliseconds to seconds, then to minutes and finally to hours. We use the Math.floor() method to round a number down to the nearest integer.

We also used the modulus (%) operator to get the remainder from the division. The modulus operator is often used with clock time (hh:mm:ss) to separate hours, minutes, and seconds.

Finally, we added leading 0s to minutes and seconds if it’s less than 10 using a ternary operator (condition ? if true : else). Ternary operators are often used as shorthand for if-else statements in JavaScript.

You can also convert milliseconds to days, weeks, months, years, etc. using the same method.

Conclusion

In this article, you learned how to convert milliseconds to hours, minutes and seconds in JavaScript. You also learned how to use the modulus operator and ternary operator in JavaScript.

If you have any questions, please post them in the comments section below.

Leave a Reply