Skip to content

Instantly share code, notes, and snippets.

@tinyfly
Created June 30, 2011 20:00
Show Gist options
  • Save tinyfly/1057075 to your computer and use it in GitHub Desktop.
Save tinyfly/1057075 to your computer and use it in GitHub Desktop.
Calculate the elapsed time between two dates
//create 1 global variable for namespacing purposes.
if (typeof tinyfly === 'undefined') {
tinyfly = {};
}
/* Handles calculating elapsed time between two dates */
tinyfly.elapsedTime = (function() {
var times = {},
timeStart, timeEnd;
/* Function: timeStart
Parameters:
name - string used as a key value to store the date object
*/
timeStart = function(name) {
times[name] = new Date();
};
/* Function: timeEnd
Parameters:
name - string used as a key value to lookup the stored startTime. Must match name passed in to timeStart
Returns: (number) the time diference in seconds to 1 decimal place
*/
timeEnd = function(name) {
return Math.round(((new Date() - times[name]) / 1000) * 10 ) / 10;
};
return {
timeStart: timeStart,
timeEnd: timeEnd
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment