I needed a function to check the difference between 2 dates in javascript. I couldn't find a suitable one, so I created this one:
function dateDiff(datepart, fromdate, todate)// datepart: 'w', 'd', 'h', 'n', 's'| Viewed 8354 times
{
datepart = datepart.toLowerCase();
var diff = todate - fromdate;
var divideBy = {
w:604800000 //1000*60*60*24*7
, d:86400000 // 1000*60*60*24
, h:3600000 // 1000*60*60
, n:60000 // 1000*60
, s:1000
};
return Math.floor(diff/divideBy[datepart]);
}
#1 by Dan G. Switzer, II - July 8, 2010 at 6:15 PM
The overhead may not be much on a single call, but considering you're doing 5 calculation on each call to the function and the values don't change, it'll be more efficient to just hardcode the values and leave the equation as a comment for reference.
#2 by Paul Klinkenberg - July 9, 2010 at 1:36 PM
Regards, Paul
#3 by Dave Stillman - September 28, 2011 at 12:47 AM