Last updated: Mar 6, 2024
Reading timeยท3 min
To convert minutes to hours and minutes:
60
.hh:mm
.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
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.
console.log(Math.floor(3.99)); // ๐๏ธ 3 console.log(Math.floor(3.01)); // ๐๏ธ 3 console.log(Math.floor(3)); // ๐๏ธ 3
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.
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
).
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.
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.
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
Alternatively, you can return an object containing the results from the conversion.
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));
You can access the hours
and minutes
properties directly on the object that
the function returns.
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.
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
You can learn more about the related topics by checking out the following tutorials: