Skip to content

Instantly share code, notes, and snippets.

@JoshOldenburg
Created April 29, 2013 22:53
Show Gist options
  • Save JoshOldenburg/5485454 to your computer and use it in GitHub Desktop.
Save JoshOldenburg/5485454 to your computer and use it in GitHub Desktop.
Turn amount of seconds into a pretty formatted string
function formatPlural(val, singular, plural) {
if (val == 1) return val + singular;
return val + plural;
}
function secondsToString(seconds) {
var time = Math.round(seconds / 60);
if (time >= (60 * 24)) {
var days = Math.floor(time / (24 * 60));
var remaining = Math.floor(time % (24 * 60));
return this.formatPlural(days, 'day', 'days') + ', ' + this.minutesToString(remaining);
} else if (time >= 60) {
var hours = Math.floor(time / 60);
var minutes = time % 60;
return this.formatPlural(hours, 'hr', 'hrs') + ', ' + this.minutesToString(minutes);
} else {
return this.formatPlural(time, 'min', 'min');
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment