Created
April 30, 2013 00:40
-
-
Save JoshOldenburg/5485891 to your computer and use it in GitHub Desktop.
Minutes to pretty formatted 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
function formatPlural(val, singular, plural) { | |
if (val == 1) return val + '' + singular; | |
return val + '' + plural; | |
} | |
function minToStr(time) { | |
if (time >= (60 * 24 * 7)) { | |
var weeks = Math.floor(time / (60 * 24 * 7)); | |
var remaining = Math.floor(time % (60 * 24 * 7)); | |
return formatPlural(weeks, ' week', ' weeks') + ((remaining > 0) ? (', ' + minToStr(remaining)) : ''); | |
} else if (time >= (60 * 24)) { | |
var days = Math.floor(time / (60 * 24)); | |
var remaining = Math.floor(time % (60 * 24)); | |
return formatPlural(days, ' day', ' days') + ((remaining > 0) ? (', ' + minToStr(remaining)) : ''); | |
} else if (time >= 60) { | |
var hours = Math.floor(time / 60); | |
var remaining = time % 60; | |
return formatPlural(hours, ' hour', ' hours') + ((remaining > 0) ? (', ' + minToStr(remaining)) : ''); | |
} else { | |
return formatPlural(time, ' minute', ' minutes'); | |
} | |
} |
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 formatPlural(val, singular, plural) { | |
if (val == 1) return val + '' + singular; | |
return val + '' + plural; | |
} | |
function minToStr(time) { | |
if (time >= (60 * 24 * 7)) { | |
var weeks = Math.floor(time / (60 * 24 * 7)); | |
var remaining = Math.floor(time % (60 * 24 * 7)); | |
return formatPlural(weeks, 'week', 'weeks') + ((remaining > 0) ? (', ' + minToStr(remaining)) : ''); | |
} else if (time >= (60 * 24)) { | |
var days = Math.floor(time / (60 * 24)); | |
var remaining = Math.floor(time % (60 * 24)); | |
return formatPlural(days, 'day', 'days') + ((remaining > 0) ? (', ' + minToStr(remaining)) : ''); | |
} else if (time >= 60) { | |
var hours = Math.floor(time / 60); | |
var remaining = time % 60; | |
return formatPlural(hours, 'hr', 'hrs') + ((remaining > 0) ? (', ' + minToStr(remaining)) : ''); | |
} else { | |
return formatPlural(time, 'min', 'min'); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment