Created
September 28, 2012 00:52
-
-
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).
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
/** | |
* @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