Created
February 6, 2015 18:57
-
-
Save deanapeterson/ff80b487840a39f8b08d to your computer and use it in GitHub Desktop.
convert minutes integer into hour string.
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
//usage | |
//minutesToHours(60) = '1:00' | |
//minutesToHours(45) = '0:45' | |
//minutesToHours(43) = '0:45' rounds to nearest 15 | |
function minutesToHours(minutes){ | |
var isNegative = (minutes < 0); | |
var absMinutes = Math.abs(minutes); //if negative convert to positive | |
var dividedBy60 = (absMinutes / 60); | |
var roundedHour = Math.floor(dividedBy60); | |
var minutesLeft = (absMinutes % 60); | |
var roundedMinutes = roundMinutes(minutesLeft, 15); //nearest 15 | |
var resultMinutes; | |
var result; | |
if(minutesLeft > 30 && roundedMinutes === 0){ | |
roundedHour = roundedHour + 1; | |
resultMinutes = "00"; | |
} else { | |
resultMinutes = roundedMinutes.toString(); | |
//prepend leading "0" | |
if(resultMinutes.length === 1){ | |
resultMinutes = "0" + resultMinutes; | |
} | |
} | |
result = (roundedHour).toString() + ":" + resultMinutes; | |
if(isNegative){ | |
result = "-" + result; | |
} | |
return result; | |
function roundMinutes(minutes, roundMinutesTo){ | |
var midPoint = (roundMinutesTo / 2); | |
return ( ( ( ( (minutes + midPoint) / roundMinutesTo) | 0 ) * roundMinutesTo) % 60); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment