Counting Months

I need to know the number of months between dateA and dateB. I’ve tried the I2be plugin, but it seems to cap out at days – which isn’t as exact as i need. I’d hate to write something if there is a feature/tool/existing code for this. Anyone have something?

What about

var diffMonths = dateB.getMonths() - dateA.getMonths()

Of course you have to take care of some more things like which date is bigger and is there more than a year of difference.

Sorry: it’s getMonth() not getMonths(). See Date.prototype.getMonth() - JavaScript | MDN

You could use something simple like this:

var diffMonths 	= ((dateB.getFullYear() - dateA.getFullYear())*12) + (dateB.getMonth() - dateA.getMonth());

Hope this helps.

Man, that was easier than I had imagined it. THANKS SO MUCH!

One more hint: in Java, months are “zero-based”. That means January is 0, December is 11. Just in case you use this function for other purposes…