Here is the approach:
- Subtract the year values from both the dates and multiply the result by 12.
- Subtract the month values from both the dates
- Subtract the day values from both the dates and divide the result by 30
- Add all the above values, the integer part of it as the answer.
var startdate = new Date('2011-05-01');
var enddate = new Date('2021-04-01');
var MonthDifference =
//Difference in years, multiplied by 12.
((enddate.getFullYear() - startdate.getFullYear()) * 12) +
//Difference in months.
(enddate.getMonth() - startdate.getMonth()) +
//integer value of( Difference in days, divided by 30.
Math.floor((enddate.getDate() - startdate.getDate()) / 30);
1. Math.floor() function is used to round a number downward to its nearest integer value.
2. The above method does not return the exact month difference. It only returns the completed months value.
3. Online tool for calculating the same can be found at here.