Convert Milliseconds to Hours, Minutes, Seconds in JS

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
6 min

banner

# Table of Contents

  1. Convert Milliseconds to Hours, Minutes, Seconds in JS
  2. Convert Milliseconds to Hours and Minutes in JavaScript
  3. Convert Milliseconds to Minutes and Seconds in JavaScript

# Convert Milliseconds to Hours, Minutes, Seconds in JS

To convert milliseconds to hours, minutes, seconds:

  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 are less than 10 to format them consistently.
index.js
// โœ… You can use a Quick one-liner hack const ms = 54000000; console.log(new Date(ms).toISOString().slice(11, 19)); // ๐Ÿ‘‰๏ธ 15:00:00 // ------------------------------------------------ // โœ… Or create a reusable function function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function convertMsToTime(milliseconds) { let seconds = Math.floor(milliseconds / 1000); let minutes = Math.floor(seconds / 60); let hours = Math.floor(minutes / 60); seconds = seconds % 60; 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)}:${padTo2Digits( seconds, )}`; } console.log(convertMsToTime(54000000)); // ๐Ÿ‘‰๏ธ 15:00:00 (15 hours) console.log(convertMsToTime(86400000)); // ๐Ÿ‘‰๏ธ 00:00:00 (24 hours) console.log(convertMsToTime(36900000)); // ๐Ÿ‘‰๏ธ 10:15:00 (10 hours, 15 minutes) console.log(convertMsToTime(15305000)); // ๐Ÿ‘‰๏ธ 04:15:05 (4 hours, 15 minutes, 5 seconds)

convert milliseconds to hours minutes seconds

The code for this article is available on GitHub

If you need to Convert Milliseconds to Hours and Minutes, scroll down to the next subheading

We created a reusable function that takes milliseconds as a parameter and returns the hours, minutes and seconds representation, formatted as hh:mm:ss.

The first thing we did is create a padTo2Digits function that will take care of adding a leading zero if the hours, minutes or seconds only contain a single digit (are less than 10).
index.js
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } console.log(padTo2Digits(3)); // ๐Ÿ‘‰๏ธ '03' console.log(padTo2Digits(6)); // ๐Ÿ‘‰๏ธ '06' console.log(padTo2Digits(10)); // ๐Ÿ‘‰๏ธ '10'

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

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.
By default, the function rolls the hours over if greater than 24, e.g. 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.

The last step is to format the values for the hours, minutes and seconds in a way that suits your use case.

We formatted them as hh:mm:ss by adding a leading zero if the value is less than 10. However, you can tweak the return value of the function depending on your use case.

# 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.
index.js
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)

convert milliseconds to hours and minutes

The code for this article is available on GitHub

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 sample 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 on your use case.

# Convert Milliseconds to Minutes and Seconds in JavaScript

To convert milliseconds to minutes and seconds:

  1. Divide the milliseconds by 60000 to convert them to minutes.
  2. Convert the remainder to seconds.
  3. Pad the seconds to two digits if the value is less than 10.
index.js
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function convertMsToMinutesSeconds(milliseconds) { const minutes = Math.floor(milliseconds / 60000); const seconds = Math.round((milliseconds % 60000) / 1000); return seconds === 60 ? `${minutes + 1}:00` : `${minutes}:${padTo2Digits(seconds)}`; } console.log(convertMsToMinutesSeconds(296900)); // ๐Ÿ‘‰๏ธ 4:57 console.log(convertMsToMinutesSeconds(296499)); // ๐Ÿ‘‰๏ธ 4:56 console.log(convertMsToMinutesSeconds(992000)); // ๐Ÿ‘‰๏ธ 16:32

convert milliseconds to minutes and seconds

The code for this article is available on GitHub

We created a reusable function, which takes the milliseconds as a parameter and converts them to minutes and seconds.

The first thing we did was create a padTo2Digits function, which will take care of adding a leading zero if the seconds only contain a single digit (are less than 10).

We want to make sure the result does not alternate between single and double digit values depending on the seconds.

In our convertMsToTime function, we:

  1. Converted the milliseconds to minutes.
  2. Converted the remainder to seconds.
If the value for the seconds is equal to 60, we have to add 1 minute to the minutes and set the seconds back to 0.

In any other case, we return the minutes and use the padTo2Digits function to pad the seconds to 2 digits if their value is less than 10.

index.js
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } console.log(padTo2Digits(2)); // ๐Ÿ‘‰๏ธ '02' console.log(padTo2Digits(4)); // ๐Ÿ‘‰๏ธ '04' console.log(padTo2Digits(10)); // ๐Ÿ‘‰๏ธ '10'

You can also use this approach to pad the minutes if you want the string to contain at least 2 digits for the minutes as well.

index.js
function padTo2Digits(num) { return num.toString().padStart(2, '0'); } function convertMsToMinutesSeconds(milliseconds) { const minutes = Math.floor(milliseconds / 60000); const seconds = Math.round((milliseconds % 60000) / 1000); return seconds === 60 ? `${padTo2Digits(minutes + 1)}:00` : `${padTo2Digits(minutes)}:${padTo2Digits(seconds)}`; } console.log(convertMsToMinutesSeconds(296900)); // ๐Ÿ‘‰๏ธ 04:57 console.log(convertMsToMinutesSeconds(296499)); // ๐Ÿ‘‰๏ธ 04:56 console.log(convertMsToMinutesSeconds(992000)); // ๐Ÿ‘‰๏ธ 16:32
The code for this article is available on GitHub

We used the padTo2Digits to add a leading zero if the minutes are less than 10.

The function returns a string that is formatted as mm:ss, but you could tweak this depending on your use case.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

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.

Copyright ยฉ 2024 Borislav Hadzhiev