Convert Minutes to Hours and Minutes in JavaScript

avatar
Borislav Hadzhiev

Last updated: Mar 6, 2024
3 min

banner

# Convert Minutes to Hours and Minutes in JavaScript

To convert minutes to hours and minutes:

  1. Get the number of full hours by dividing the minutes by 60.
  2. Get the remainder of the minutes.
  3. Optionally, format the hours and minutes as hh:mm.
index.js
function toHoursAndMinutes(totalMinutes) { const minutes = totalMinutes % 60; const hours = Math.floor(totalMinutes / 60); return `${padTo2Digits(hours)}:${padTo2Digits(minutes)}`; } function padTo2Digits(num) { return num.toString().padStart(2, '0'); } console.log(toHoursAndMinutes(90)); // ๐Ÿ‘‰๏ธ 01:30 console.log(toHoursAndMinutes(60)); // ๐Ÿ‘‰๏ธ 01:00 console.log(toHoursAndMinutes(115)); // ๐Ÿ‘‰๏ธ 01:55

convert minutes to hours and minutes

The code for this article is available on GitHub

We created a reusable function, which takes a number that represents the total minutes as a parameter and converts them to hours and minutes.

The first step is to get the number of full hours, by dividing the total minutes by 60 and rounding the result down.

The Math.floor() method rounds a number down if the number has a decimal, otherwise, it returns the number as is.

index.js
console.log(Math.floor(3.99)); // ๐Ÿ‘‰๏ธ 3 console.log(Math.floor(3.01)); // ๐Ÿ‘‰๏ธ 3 console.log(Math.floor(3)); // ๐Ÿ‘‰๏ธ 3
This ensures we don't get values with a decimal, e.g. 1.336 for the hours. If the value has a decimal, we want to show the minutes and hide the decimal.

We used the modulo (%) operator to get the remainder of the minutes.

index.js
console.log(90 % 60); // ๐Ÿ‘‰๏ธ 30

The example above shows that the remainder of dividing 90 by 60 is 30, in other words, 1 full hour and 30 minutes.

The next step is to format the hours and minutes as hh:mm, e.g. 09:35.

Our padTo2Digits function, takes care of adding a leading zero if the hours or minutes 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 does not alternate between single and double digit values depending on the hours and minutes.

The example from the code snippet formats the values as hh:mm, but you could tweak this depending on your use case.

Here is another example.

index.js
function toHoursAndMinutes(totalMinutes) { const minutes = totalMinutes % 60; const hours = Math.floor(totalMinutes / 60); return `${hours}h${minutes > 0 ? ` ${minutes}m` : ''}`; } console.log(toHoursAndMinutes(90)); // ๐Ÿ‘‰๏ธ 1h 30m console.log(toHoursAndMinutes(60)); // ๐Ÿ‘‰๏ธ 1h console.log(toHoursAndMinutes(115)); // ๐Ÿ‘‰๏ธ 1h 55m

The function displays the abbreviated values for the hours and minutes.

We used the ternary operator to display the value for the minutes only if they are more than 0.

You can always display the minutes by removing the condition.

index.js
function toHoursAndMinutes(totalMinutes) { const minutes = totalMinutes % 60; const hours = Math.floor(totalMinutes / 60); return `${hours}h ${minutes}m`; } console.log(toHoursAndMinutes(60)); // ๐Ÿ‘‰๏ธ 1h 0m console.log(toHoursAndMinutes(90)); // ๐Ÿ‘‰๏ธ 1h 30m console.log(toHoursAndMinutes(115)); // ๐Ÿ‘‰๏ธ 1h 55m

# Convert Minutes to Hours and Minutes without formatting

Alternatively, you can return an object containing the results from the conversion.

index.js
function toHoursAndMinutes(totalMinutes) { const minutes = totalMinutes % 60; const hours = Math.floor(totalMinutes / 60); return {hours, minutes}; } // ๐Ÿ‘‡๏ธ { hours: 1, minutes: 30 } console.log(toHoursAndMinutes(90)); // ๐Ÿ‘‡๏ธ { hours: 1, minutes: 0 } console.log(toHoursAndMinutes(60)); // ๐Ÿ‘‡๏ธ { hours: 1, minutes: 55 } console.log(toHoursAndMinutes(115));

convert minutes to hours and minutes without formatting

The code for this article is available on GitHub

You can access the hours and minutes properties directly on the object that the function returns.

index.js
function toHoursAndMinutes(totalMinutes) { const minutes = totalMinutes % 60; const hours = Math.floor(totalMinutes / 60); return {hours, minutes}; } const result = toHoursAndMinutes(90); console.log(result.hours); // ๐Ÿ‘‰๏ธ 1 console.log(result.minutes); // ๐Ÿ‘‰๏ธ 30

Alternatively, you can destructure the values and assign them to variables in a single statement.

index.js
function toHoursAndMinutes(totalMinutes) { const minutes = totalMinutes % 60; const hours = Math.floor(totalMinutes / 60); return {hours, minutes}; } const {hours, minutes} = toHoursAndMinutes(90); console.log(hours); // ๐Ÿ‘‰๏ธ 1 console.log(minutes); // ๐Ÿ‘‰๏ธ 30

destructure hours minutes and assign them to variables

The code for this article is available on GitHub

# 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