Skip to content

Instantly share code, notes, and snippets.

@railsstudent
Created January 8, 2017 11:02
Show Gist options
  • Save railsstudent/5753ffbc274d23c82f5e6ab43b51a6b1 to your computer and use it in GitHub Desktop.
Save railsstudent/5753ffbc274d23c82f5e6ab43b51a6b1 to your computer and use it in GitHub Desktop.
function buildString(value, unit) {
if (value > 0) {
return value + ' ' + unit + (value > 1 ? 's' : '') + ', ';
}
return '';
}
function formatDuration (seconds) {
// Complete this function
if (seconds === 0) {
return "now";
}
var tmpSeconds = seconds;
const NUM_SEC_PER_MIN = 60;
const NUM_SEC_PER_HR = NUM_SEC_PER_MIN * 60;
const NUM_SEC_PER_DAY = NUM_SEC_PER_HR * 24;
const NUM_SEC_PER_YEAR = NUM_SEC_PER_DAY * 365;
var numYears = Math.floor(tmpSeconds / NUM_SEC_PER_YEAR);
tmpSeconds -= numYears * NUM_SEC_PER_YEAR;
var numDays = Math.floor(tmpSeconds / NUM_SEC_PER_DAY);
tmpSeconds -= numDays * NUM_SEC_PER_DAY;
var numHrs = Math.floor(tmpSeconds / NUM_SEC_PER_HR);
tmpSeconds -= numHrs * NUM_SEC_PER_HR;
var numMins = Math.floor(tmpSeconds / NUM_SEC_PER_MIN);
var numSecs = tmpSeconds - numMins * NUM_SEC_PER_MIN;
var formattedStr = buildString(numYears, 'year');
formattedStr += buildString(numDays, 'day');
formattedStr += buildString(numHrs, 'hour');
formattedStr += buildString(numMins, 'minute');
formattedStr += buildString(numSecs, 'second');
// trim last one
var idx = formattedStr.lastIndexOf(', ');
formattedStr = formattedStr.substring(0, idx);
// replace the second last , with and
idx = formattedStr.lastIndexOf(', ');
if (idx > 0) {
formattedStr = formattedStr.substring(0, idx) + ' and ' + formattedStr.substring(idx + 2);
}
return formattedStr;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment