Created
June 2, 2024 22:32
-
-
Save MateoWartelle/bebcfd2b46c621e9ea51ac518b188bb7 to your computer and use it in GitHub Desktop.
Time convert number in X hours and X minutes
This file contains hidden or 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
export function timeConvert(n: any) { | |
// Store the input number of minutes in a variable num | |
var num = n; | |
// Calculate the total hours by dividing the number of minutes by 60 | |
var hours = num / 60; | |
// Round down the total hours to get the number of full hours | |
var rhours = Math.floor(hours); | |
// Calculate the remaining minutes after subtracting the full hours from the total hours | |
var minutes = (hours - rhours) * 60; | |
// Round the remaining minutes to the nearest whole number | |
var rminutes = Math.round(minutes); | |
// Construct and return a string representing the conversion result | |
return ( | |
(rhours > 0 ? rhours + " hour" : "") + | |
(rhours > 1 ? "s" : "") + | |
(rminutes > 0 | |
? (rhours > 0 ? " and " : "") + | |
rminutes + | |
" minute" + | |
(rminutes > 0 ? "s" : "") | |
: "") | |
); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment