Skip to content

Instantly share code, notes, and snippets.

@mattbontrager
Created September 28, 2012 00:52
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mattbontrager/3797355 to your computer and use it in GitHub Desktop.
Save mattbontrager/3797355 to your computer and use it in GitHub Desktop.
Transposing an estimated time span into milliseconds (and optionally humanizing the duration using moment.js).
/**
* @param etaTime Integer
* @param etaScope String
* @param True/False/Boolean
**/
function calculateDuration(etaTime, etaScope, humanize) {
if (typeof etaTime !== 'number') {
etaTime = parseInt(etaTime, 10);
}
var multiplier, combinedTime,
millisecond = 1,
second = millisecond * 1000,
minute = (second * 60),
hour = (minute * 60),
day = (hour * 24),
week = (day * 7),
month = (week * 4),
year = (month * 12);
if (etaScope === ('minutes' || 'minute')) {
multiplier = minute;
}
else if (etaScope === ('hours' || 'hour')) {
multiplier = hour;
}
else if (etaScope === ('days' || 'day')) {
multiplier = day;
}
else if (etaScope === ('weeks' || 'week')) {
multiplier = week;
}
else if (etaScope === ('months' || 'month')) {
multiplier = month;
}
else {
multiplier = year;
}
if (humanize) {
combinedTime = moment.humanizeDuration(etaTime * multiplier);
}
else {
combinedTime = etaTime * multiplier;
}
return combinedTime;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment