The definition of "number of months in difference" is subject to many interpretations. :-)
You can get the year, month, and day of the month from a JavaScript date object. Depending on what information you are looking for, you can use them to determine the number of months between two points in time.
For example, outside the cuff, this finds out how many full months lie between two dates, not counting partial months (for example, except for the month in which each date is located):
function monthDiff(d1, d2) { var months; months = (d2.getFullYear() - d1.getFullYear()) * 12; months -= d1.getMonth() + 1; months += d2.getMonth(); return months <= 0 ? 0 : months; } monthDiff( new Date(2008, 10, 4), // November 4th, 2008 new Date(2010, 2, 12) // March 12th, 2010 ); // Result: 15: December 2008, all of 2009, and Jan & Feb 2010 monthDiff( new Date(2010, 0, 1), // January 1st, 2010 new Date(2010, 2, 12) // March 12th, 2010 ); // Result: 1: February 2010 is the only full month between them monthDiff( new Date(2010, 1, 1), // February 1st, 2010 new Date(2010, 2, 12) // March 12th, 2010 ); // Result: 0: There are no *full* months between them
(Note that the month values ββin JavaScript start with 0 = January.)
Including fractional months in the foregoing is much more difficult, because three days in a typical February make up the greater part of this month (~ 10.714%) than three days in August (~ 9.677%), and, of course, even February a moving target, depending on is he a leap year.
There are also some date and time libraries for JavaScript that probably simplify this task.