Convert Minutes to Hours and Minutes in JavaScript

Suppose you are looking for a way to convert Minutes to Hours and Minutes in JavaScript. Read this article to the end, and you will find the right solution.

Convert Minutes to Hours and Minutes in JavaScript.

There are many ways to convert Minutes to Hours and Minutes in JavaScript. Some of them are:

Using operator ‘/’ and ‘%’

Here, to convert minutes to hours and minutes, we need to get a number divided by 60, surround the result to get a full hour and get a number divided by the remainder by 60 to get minutes.

First, we will get a number divided by 60 and surround the result using Math. floor() method.

Code example:

console.log("The number is not surrouding:", 135/60) // 2.25
console.log("The number is not surrouding:", Math.floor(135/60)) // 2

Output:

The number is not surrouding: 2.25
The number is not surrouding: 2

Second, we will use the operator to get the remainder of division.

Code example:

console.log(135 % 60) // 15

Output:

15

So, I have the complete code to convert Minutes to Hours and Minutes in JavaScript.

Code example:

// The function to return hours and minutes
function get_hours_and_minutes(total_minutes) {
const hours = Math.floor(total_minutes / 60);
const minutes = total_minutes % 60;

return ${covert_2_digits(hours)}:${covert_2_digits(minutes)};
}

// The function to return 2 digits
function covert_2_digits(numbers) {
}

let num1 = 100;
let num2 = 150;
let num3 = 360;

console.log(num1 + " convert to hours and minutes: " + get_hours_and_minutes(num1));
console.log(num2 + " convert to hours and minutes: " + get_hours_and_minutes(num2));
console.log(num3 + " convert to hours and minutes: " + get_hours_and_minutes(num3));

Output:

100 convert to hours and minutes: 01:40
150 convert to hours and minutes: 02:30
360 convert to hours and minutes: 06:00

Using for loop and subtraction

Besides getting hours by division we can also get hours by subtraction through a for loop. Then, we total minutes – a number multiplied by 60, we will receive a minute.

Code example:

// The function to return hours and minutes
function get_hours_and_minutes(total_minutes) {
let hours = 0;
let temp = total_minutes;

for(let i = 0; i < temp; i++){
if(temp - hours*60 < 0){
hours = hours - 1;
break;
}
else{
hours = hours + 1;
}
}

let minutes = total_minutes - hours*60;
return ${covert_2_digits(hours)}:${covert_2_digits(minutes)};
}

// The function to return 2 digits
function covert_2_digits(numbers) {
}

let num1 = 100;
let num2 = 150;
let num3 = 360;

console.log(num1 + " convert to hours and minutes: " + get_hours_and_minutes(num1));
console.log(num2 + " convert to hours and minutes: " + get_hours_and_minutes(num2));
console.log(num3 + " convert to hours and minutes: " + get_hours_and_minutes(num3));

Output:

100 convert to hours and minutes: 01:40
150 convert to hours and minutes: 02:30
360 convert to hours and minutes: 06:00

Summary

So we have covered all the solutions to convert Minutes to Hours and Minutes in JavaScript. If you have any questions, please leave a comment below.

Have a good day and see you later!

Maybe you are interested: