Created
January 2, 2023 03:20
-
-
Save foxtbirdy/dbb0a8982f3155490e503a7c6c7a9139 to your computer and use it in GitHub Desktop.
Converting minutes to hours, minutes using JavaScript
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function timeConverter1(totalMinutes) { | |
const hours = Math.trunc(totalMinutes / 60); | |
const minutes = totalMinutes % 60; | |
return { hours, minutes }; | |
} | |
// {hours: 3, minutes: 20} | |
console.log(timeConverter1(200)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function toHoursAndMinutes(totalMinutes) { | |
const hours = Math.floor(totalMinutes / 60); | |
const minutes = totalMinutes % 60; | |
return { hours, minutes }; | |
} | |
// {hours: 3, minutes: 20} | |
console.log(toHoursAndMinutes(200)); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function timeConverter2(totalMinutes) { | |
const inHours = totalMinutes / 60; | |
const hours = totalMinutes >= 60 ? Math.trunc(inHours) : ""; | |
const minute = | |
inHours % 1 === 0 ? "" : +(inHours.toFixed(2) + "").split(".")[1]; | |
const minutes = +((60 / 100) * minute).toFixed(); | |
return { hours, minutes }; | |
} | |
// {hours: 3, minutes: 20} | |
console.log(timeConverter2(200)); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment