How to Convert Minutes to Hours and Minutes in JavaScript
A common task in programming is to convert a total number of minutes into a more human-readable format of hours and minutes (e.g., converting 75 minutes to 1 hour, 15 minutes). This is essential for displaying durations, calculating time, or formatting reports.
This guide will teach you the simple and reliable mathematical logic for this conversion using division and the modulo operator. You will learn how to create a reusable function that returns both hours and minutes, and then see how to format that result into different string representations.
The Core Logic: Division and the Modulo Operator
The conversion from total minutes to hours and remaining minutes relies on two simple mathematical operations:
- Get the Hours: To find the number of full hours, divide the total minutes by
60and round the result down to the nearest whole number.Math.floor()is perfect for this. - Get the Remaining Minutes: To find the minutes that are "left over" after accounting for the full hours, use the modulo operator (
%).totalMinutes % 60returns the remainder of the division.
Example:
const totalMinutes = 75;
// 1. Get the hours
const hours = Math.floor(totalMinutes / 60);
console.log(hours); // Output: 1
// 2. Get the remaining minutes
const minutes = totalMinutes % 60;
console.log(minutes); // Output: 15
The Reusable Function (Best Practice): Returning an Object
For the most flexible and reusable code, you should encapsulate this logic in a function that returns an object containing the hours and minutes. This allows the calling code to decide how to use or format the result.
For example, we need a clean way to convert any number of minutes into its hour and minute components. How to convert minutes into hours and minutes?
// Problem: How to convert 75 minutes into hours and minutes?
const totalMinutes = 75;
The solution:
function getHoursAndMinutes(totalMinutes) {
const hours = Math.floor(totalMinutes / 60);
const minutes = totalMinutes % 60;
return { hours, minutes };
}
// Example Usage:
const result = getHoursAndMinutes(75);
console.log(result); // Output: { hours: 1, minutes: 15 }
// You can easily access the individual components
console.log(`Hours: ${result.hours}`);
console.log(`Minutes: ${result.minutes}`);
Formatting the Result as a String
Once you have the result object, you can format it into any string representation you need.
Formatting as hh:mm
A common requirement is to format the duration as a zero-padded, two-digit string (e.g., 01:09).
We can create a helper function to pad the numbers with a leading zero and then build the string.
function getFormattedTime(totalMinutes) {
const { hours, minutes } = getHoursAndMinutes(totalMinutes);
// Helper to pad a number to 2 digits
const padTo2Digits = (num) => String(num).padStart(2, '0');
return `${padTo2Digits(hours)}:${padTo2Digits(minutes)}`;
}
// Example Usage:
console.log(getFormattedTime(75)); // Output: "01:15"
console.log(getFormattedTime(69)); // Output: "01:09"
console.log(getFormattedTime(120)); // Output: "02:00"
Formatting as 1h 30m
Another common format is a more descriptive string.
function getDescriptiveTime(totalMinutes) {
const { hours, minutes } = getHoursAndMinutes(totalMinutes);
let result = `${hours}h`;
if (minutes > 0) {
result += ` ${minutes}m`;
}
return result;
}
// Example Usage:
console.log(getDescriptiveTime(75)); // Output: "1h 15m"
console.log(getDescriptiveTime(120)); // Output: "2h"
Conclusion
Converting a total number of minutes into an hours-and-minutes format is a simple mathematical task in JavaScript.
- The core logic involves division and
Math.floor()to get the hours, and the modulo operator (%) to get the remaining minutes. - The best practice is to create a reusable function that returns an object (
{ hours, minutes }). This separates the calculation from the presentation. - You can then create additional formatting functions to convert this object into any user-facing string format you need, such as
hh:mmor1h 30m.