440,496 Members | 1,528 Online Need help? Post your question and get tips & solutions from a community of 440,496 IT Pros & Developers. It's quick & easy.

# JavaScript Date Difference, accounting for Leap Years

 P: n/a Hi, I'm really sorry to post this as I know it must have been asked countless times before, but I can't find an answer anywhere. Does anyone have a snippet of JavaScript code I could borrow which calculated the difference in years and days between two dates, and takes leap years into account? I'm calculating the difference in the usual way, i.e.... var difference = dateTo.getTime() - dateFrom.getTime(); ....and converting this millisecond value into days by using... var daysDifference = (difference/1000/60/60/24); But how do I then display the difference in days AND years? I've tried the following: var yearsDifference = Math.floor(daysDifference/365.25); var daysLeft = Math.floor(daysDifference-(yearsDifference*365.25)); ....but it gives me inaccuracies. For example, if I use my code to calculate the difference between 05/01/1998 and 05/01/2000 it returns 1 year and 364 days! Any assistance would be gratefully received! Jun 19 '06 #1
4 Replies

 P: n/a ja********@hotmail.com writes: Does anyone have a snippet of JavaScript code I could borrow which calculated the difference in years and days between two dates, and takes leap years into account? That requires a more precise description of what you want. I'm calculating the difference in the usual way, i.e.... var difference = dateTo.getTime() - dateFrom.getTime(); ...and converting this millisecond value into days by using... var daysDifference = (difference/1000/60/60/24); This fails if the dates are using local time and the difference crosses a daylight saving time boundary (i.e., a day that is not 24 hours long). At least round it to the nearest integer. But how do I then display the difference in days AND years? How many days and years are there between 2004-02-28 and 2005-02-28 ? How many days and years between 2004-02-29 and 2005-02-28 ? How many days and years between 2004-02-29 and 2005-03-01 ? My immediate guesses would be, respectively: one year, zero days zero years, 365 days one year, one day How come moving both the start date and the end date one day forwards changes the distance between them? And what is the day that is one year and zero days after 2004-02-29? If there is no day that is one year and zero days after 2004-02-29, how long is a year? The problem I'm trying to illustrate is that you are talking about periods of some years and some days, when years are not a fixed number of days. I've tried the following: var yearsDifference = Math.floor(daysDifference/365.25); var daysLeft = Math.floor(daysDifference-(yearsDifference*365.25)); ...but it gives me inaccuracies. That's a matter of definition :) For example, if I use my code to calculate the difference between 05/01/1998 and 05/01/2000 it returns 1 year and 364 days! Unsurpricingly. You are defining a year to be 365.25 days. The exact result would be a difference of one year and 364.75 days. Then you decide to round the days, but "one year" is still not a whole number of days. Any assistance would be gratefully received! What do you need it for? A quick google gives A specification of an algorithm could be: One whole number of years after a specific date ends on the same date on a later year, or, if that date does not exist (i.e., it's Feb 29th), the first following date that does (Mar 1st). The distance in years and days between two dates is the largest number of whole years after the earlier date that is still less than the larger date, plus the number of days from that date to the end date. So, algorithm: ---- function YDDiff(years,days) { this.years = years; this.days = days; } YDDiff.prototype.toString = function() { return this.years + "y" + this.days +"d"; }; function diffYearAndDate(d1,d2) { // ensure d1 <= d2 if (d1 > d2) { return diffYearAndDate(d2,d1); } // find n whole years later than d1 in d2's year. var dt = new Date(d1); dt.setFullYear(d2.getFullYear()); // n whole years later > d2 var overflow = (dt > d2); // max whole years later than d1 less than d2 dt = new Date(d1); dt.setFullYear(d2.getFullYear() - overflow); // whole years from d1 to dt var years = dt.getFullYear() - d1.getFullYear(); // days from dt to d2, less than whole year from dt var days = Math.round((d2 - dt)/864e5); return new YDDiff(years,days); } ---- Now you'll just have to see if that is what you really need :) /L -- Lasse Reichstein Nielsen - lr*@hotpop.com DHTML Death Colors: 'Faith without judgement merely degrades the spirit divine.' Jun 19 '06 #2 