Manipulating dates in Javascript is an extremely common requirement for a large number of apps and server logic (when using NodeJs). The ability to retrieve the number of months between dates is a very common and useful skill to have in your toolkit.

To do this, we need both the month and year components of the two dates. We then calculate the number of months between the month components and add the difference in years multiplied by 12, which converts the years to months.

This method does not take into account partial amounts of the month that have progressed between the two dates (in the case above the end date is 6 days ahead so the full amount would be approximately 27.2).

If you are unable to ascertain which is the start and end dates, wrapping the monthDiff calculation with Math.abs() will always return the positive value: