How to Convert Minutes to Hours and Minutes, Alternate Method #3285
-
df.minutesToHours(175) => 2 // 2 hours How to convert and return Hours and Minutes like 2:55 ?
|
Beta Was this translation helpful? Give feedback.
Answered by
sbulatnikov
Dec 8, 2022
Replies: 1 comment 1 reply
-
You should do it by yourself, examples below const minutesToHoursExtended = minutes => {
const hours = minutesToHours(minutes);
const minutesLeft = minutes - hours * 60;
const stringifiedHours = String(hours).length === 1 ? `0${hours}` : `${hours}`;
const stringifiedMinutes =
String(minutesLeft).length === 1 ? `0${minutesLeft}` : `${minutesLeft}`;
return `${stringifiedHours}:${stringifiedMinutes}`;
};
console.log(minutesToHoursExtended(175)); // 02:55
console.log(minutesToHoursExtended(120)); // 02:00
console.log(minutesToHoursExtended(59)); // 00:59 or without additional zeros const minutesToHoursExtendedSimplified = minutes => {
const hours = minutesToHours(minutes);
const minutesLeft = minutes - hours * 60;
return minutesLeft !== 0 ? `${hours}:${minutesLeft}` : `${hours}`;
};
console.log(minutesToHoursExtendedSimplified(175)); // 2:55
console.log(minutesToHoursExtendedSimplified(120)); // 2
console.log(minutesToHoursExtendedSimplified(59)); // 0:59 |
Beta Was this translation helpful? Give feedback.
1 reply
Answer selected by
hakimLyon
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
You should do it by yourself, examples below
with additional zeros (digital clock style)
or without additional zeros