Convert Milliseconds to Hours and Minutes in JavaScript


Borislav Hadzhiev

Last updated: Jul 25, 2022


Photo from Unsplash

Convert Milliseconds to Hours and Minutes in JavaScript #

To convert milliseconds to hours and minutes:

  1. Divide the milliseconds by 1000 to get the seconds.
  2. Divide the seconds by 60 to get the minutes.
  3. Divide the minutes by 60 to get the hours.
  4. Add a leading zero if the values of the hours and minutes are less than 10 to format them consistently.
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function convertMsToHM(milliseconds) { let seconds = Math.floor(milliseconds / 1000); let minutes = Math.floor(seconds / 60); let hours = Math.floor(minutes / 60); seconds = seconds % 60; // 👇️ if seconds are greater than 30, round minutes up (optional) minutes = seconds >= 30 ? minutes + 1 : minutes; minutes = minutes % 60; // 👇️ If you don't want to roll hours over, e.g. 24 to 00 // 👇️ comment (or remove) the line below // commenting next line gets you `24:00:00` instead of `00:00:00` // or `36:15:31` instead of `12:15:31`, etc. hours = hours % 24; return `${padTo2Digits(hours)}:${padTo2Digits(minutes)}`; } console.log(convertMsToHM(54000000)); // 👉️ 15:00 (15 hours) console.log(convertMsToHM(86400000)); // 👉️ 00:00 (24 hours) console.log(convertMsToHM(36900000)); // 👉️ 10:15 (10 hours, 15 minutes) console.log(convertMsToHM(15335000)); // 👉️ 04:16 (4 hours, 15 minutes, 35 seconds) console.log(convertMsToHM(130531000)); // 👉️ 36:16 (36 hours 15 minutes 31 seconds)

If you need to convert the milliseconds to hours, minutes and seconds, check out my other article - Convert Milliseconds to Hours, Minutes, Seconds in JS.

The convertMsToHM function takes the number of milliseconds as a parameter and converts them to hours and minutes, formatted as hh:mm (optional).

The first function in the code snippet is padTo2Digits. The function takes care of adding a leading zero if the values for the hours or minutes contain a single digit (are less than 10).

We want to make sure the result doesn't alternate between single and double digit values depending on the hour and minutes.

In our convertMsToTime function, we:

  1. Converted the milliseconds to seconds by dividing the value by 1000.
  2. Converted the seconds to minutes by dividing the value by 60.
  3. Converted the minutes to hours by dividing the value by 60.
  4. Used the modulo (%) operator to reset the values to 0 if, for example, the user passed 86400000 as the milliseconds, which is equivalent to 24 hours.

Note that we check if the seconds are greater than 30, in which case we round the minutes up.

By default, the function rolls the hours over if greater than 24. For example, if the milliseconds are 36 hours, the hours = hours % 24 line sets the hours to 12. Depending on your use case, you might not want to roll the hours over.

You can comment out the hours = hours % 24 line to not roll the hours over.

In the function, we formatted the result as hh:mm, however you can tweak this depending your your use case.

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.