Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference in Months between two dates in JavaScript

How would I work out the difference for two Date() objects in JavaScript, while only return the number of months in the difference?

Any help would be great :)

like image 245
williamtroup Avatar asked Mar 29 '10 07:03

williamtroup


People also ask

How to get difference in months between 2 dates in javascript?

To get the number of months between 2 dates: Use the getMonth() method to calculate the month difference between the two dates. Use the getFullYear() method to calculate the difference in years between the dates. Multiply the year difference by 12 and return the sum.

How do I calculate the number of years between two dates in Javascript?

var years = ((days)*(thisyear-birthyear)) /((number_of_long_years*366) + ((thisyear-birthyear-number_of_long_years)*365) );


1 Answers

The definition of "the number of months in the difference" is subject to a lot of interpretation. :-)

You can get the year, month, and day of month from a JavaScript date object. Depending on what information you're looking for, you can use those to figure out how many months are between two points in time.

For instance, off-the-cuff:

function monthDiff(d1, d2) {     var months;     months = (d2.getFullYear() - d1.getFullYear()) * 12;     months -= d1.getMonth();     months += d2.getMonth();     return months <= 0 ? 0 : months; } 

function monthDiff(d1, d2) {      var months;      months = (d2.getFullYear() - d1.getFullYear()) * 12;      months -= d1.getMonth();      months += d2.getMonth();      return months <= 0 ? 0 : months;  }    function test(d1, d2) {      var diff = monthDiff(d1, d2);      console.log(          d1.toISOString().substring(0, 10),          "to",          d2.toISOString().substring(0, 10),          ":",          diff      );  }    test(      new Date(2008, 10, 4), // November 4th, 2008      new Date(2010, 2, 12)  // March 12th, 2010  );  // Result: 16    test(      new Date(2010, 0, 1),  // January 1st, 2010      new Date(2010, 2, 12)  // March 12th, 2010  );  // Result: 2    test(      new Date(2010, 1, 1),  // February 1st, 2010      new Date(2010, 2, 12)  // March 12th, 2010  );  // Result: 1

(Note that month values in JavaScript start with 0 = January.)

Including fractional months in the above is much more complicated, because three days in a typical February is a larger fraction of that month (~10.714%) than three days in August (~9.677%), and of course even February is a moving target depending on whether it's a leap year.

There are also some date and time libraries available for JavaScript that probably make this sort of thing easier.


Note: There used to be a + 1 in the above, here:

months = (d2.getFullYear() - d1.getFullYear()) * 12; months -= d1.getMonth() + 1; // −−−−−−−−−−−−−−−−−−−−^^^^ months += d2.getMonth(); 

That's because originally I said:

...this finds out how many full months lie between two dates, not counting partial months (e.g., excluding the month each date is in).

I've removed it for two reasons:

  1. Not counting partial months turns out not to be what many (most?) people coming to the answer want, so I thought I should separate them out.

  2. It didn't always work even by that definition. :-D (Sorry.)

like image 155
T.J. Crowder Avatar answered Sep 20 '22 11:09

T.J. Crowder