Calculating The Number of Months
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:
If you liked this post, please share it!