Created
October 14, 2014 21:39
-
-
Save jillesme/f8c19942f08743cc884f to your computer and use it in GitHub Desktop.
Parses seconds to a nice displayed time :)
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 parseTime (initialTime) { | |
var time = parseInt(initialTime); | |
var result = ''; | |
if (time <= 59) { | |
result = time > 9 ? '00:' + time : '00:0' + time; | |
} else if (time > 59 && time <= 3599) { | |
var minutes = 0; | |
while (time > 59) { | |
time -= 60; | |
minutes += 1; | |
} | |
result = (minutes > 9 ? '' : '0') + minutes.toString() + ':' + ((time > 9) ? time.toString() : '0' + time.toString()); | |
} else if (time > 3599) { | |
var hours = 0; | |
var minutes = 0; | |
while (time > 3600) { | |
time -= 3600; | |
hours += 1; | |
} | |
while (time > 59) { | |
time -= 60; | |
minutes += 1; | |
} | |
result = (hours > 9 ? '' : '0') + hours.toString() + ':' + (minutes > 9 ? '' : '0') + minutes.toString() + ':' + ((time > 9) ? time.toString() : '0' + time.toString()); | |
} | |
return result || time; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment