var dtFrom= new Date('01/05/2019');
var dtTo= new Date('01/06/2021');
var fromYear;
var fromMonth = dtFrom.getMonth() + 1;
var toYear;
var toMonth = dtTo.getMonth() + 1;
if ((fromYear = dtFrom.getFullYear()) < (toYear = dtTo.getFullYear())) {
toMonth += (toYear - fromYear) * 12;
}
var diffMonths = toMonth - fromMonth;
if (dtFrom.getDate() > dtTo.getDate()) diffMonths--;
-----------------------------------------------------------------------------
For get only the month diff
var dtFrom= new Date('01/05/2019');
var dtTo= new Date('01/06/2021');
var monthCount = 0;
while ((dtFrom.getMonth() + '' + dtFrom.getFullYear()) != (dtTo.getMonth() + '' + dtTo.getFullYear())) {
monthCount++;
dtFrom.setMonth(dtFrom.getMonth() + 1);
}
monthCount = monthCount + 1;
var dtTo= new Date('01/06/2021');
var fromYear;
var fromMonth = dtFrom.getMonth() + 1;
var toYear;
var toMonth = dtTo.getMonth() + 1;
if ((fromYear = dtFrom.getFullYear()) < (toYear = dtTo.getFullYear())) {
toMonth += (toYear - fromYear) * 12;
}
var diffMonths = toMonth - fromMonth;
if (dtFrom.getDate() > dtTo.getDate()) diffMonths--;
-----------------------------------------------------------------------------
For get only the month diff
var dtFrom= new Date('01/05/2019');
var dtTo= new Date('01/06/2021');
var monthCount = 0;
while ((dtFrom.getMonth() + '' + dtFrom.getFullYear()) != (dtTo.getMonth() + '' + dtTo.getFullYear())) {
monthCount++;
dtFrom.setMonth(dtFrom.getMonth() + 1);
}
monthCount = monthCount + 1;