Skip to content

Instantly share code, notes, and snippets.

@waaronking
Last active August 29, 2015 14:08
Show Gist options
  • Save waaronking/84feef76a38b946b637b to your computer and use it in GitHub Desktop.
Save waaronking/84feef76a38b946b637b to your computer and use it in GitHub Desktop.
Time Between Two Dates
/* Returns time string between two dates in days, hours, minutes, and seconds */
var timeBetweenTwoDates = function(date1, date2) {
var seconds = Math.round((todaysDate - updateDate)/1000); //seconds
return calculateTime(seconds);
};
var calculateTime = function(seconds) {
var seconds = seconds || 0
, minutes = 0
, hours = 0
, days = 0;
if (seconds > 59) {
minutes = Math.floor(seconds / 60); // minutes
seconds = Math.floor(((seconds / 60) % 1) * 60);
if (minutes > 59) {
hours = Math.floor(minutes / 60); // hours
minutes = Math.floor(((minutes / 60) % 1) * 60);
if (hours > 23) {
days = Math.floor(hours / 24); // days
hours = Math.floor(((hours / 24) % 1) * 24);
}
}
} else {
seconds = Math.floor(seconds);
}
return ((days) ? days + " days " : "") + ((hours) ? hours + " hours " : "") + ((minutes) ? minutes + " minutes " : "") + seconds + " seconds";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment