// Adding date difference functionality to default date Object
Date.prototype.difference = function(secondDate, units) {
// Start date and end date should be actual Date objects
var dateDifference = this - secondDate;
switch (units) {
case "seconds":
dateDifference = dateDifference / 1000;
break;
case "minutes":
dateDifference = (dateDifference / 1000)/60;
break;
case "hours":
dateDifference = ((dateDifference / 1000)/60)/60;
break;
case "days":
dateDifference = (((dateDifference / 1000)/60)/60)/24;
break;
case "months":
dateDifference = this.getMonth() - secondDate.getMonth();
break;
}
return dateDifference +" " +units;
}
var date1 = new Date('7/25/2010');
var date2 = new Date('12/12/2010');
// Since date is a static object you will always need to access it using .prototype in the call
var interval = date2.difference(date1, "days");
console.log(interval);
This Pen doesn't use any external CSS resources.
This Pen doesn't use any external JavaScript resources.