How to Convert Minutes to Hours Minutes Seconds javaScript

Опубликовано: 25 Июль 2026
на канале: Tuts Make
181
0

There are a few ways to convert minutes to hours, minutes, and seconds in JavaScript. Here are a few examples:

// This function takes in a number of minutes and returns the number of hours in that many minutes.
function minutesToHours(minutes) {
return minutes / 60;
}

// This function takes in a number of minutes and returns an object with the hours, minutes, and seconds.
function minutesToHMS(minutes) {
const hours = Math.floor(minutes / 60);
const minutes = minutes % 60;
const seconds = minutes * 60;
return { hours, minutes, seconds };
}

// This function takes in a number of minutes and returns a string in the format "HH:MM:SS".
function minutesToHMSStr(minutes) {
const { hours, minutes, seconds } = minutesToHMS(minutes);
return `${hours}:${minutes}:${seconds}`;
}

// Example usage:
const minutes = 150;
const hours = minutesToHours(minutes);
const hms = minutesToHMS(minutes);
const hmsStr = minutesToHMSStr(minutes);

console.log(hours); // 2.5
console.log(hms); // { hours: 2, minutes: 30, seconds: 0 }
console.log(hmsStr); // "02:30:00"