Skip to content

Instantly share code, notes, and snippets.

@JoshOldenburg
Created April 30, 2013 00:40
Show Gist options
  • Save JoshOldenburg/5485891 to your computer and use it in GitHub Desktop.
Save JoshOldenburg/5485891 to your computer and use it in GitHub Desktop.
Minutes to pretty formatted string
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');
}
}
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