In case you just care about the month and year and want to touch both dates (for example you want to go thru JAN/2021 to AGO/2022) you can use this:
int numberOfMonths= (Year2 > Year1 ? ( Year2 - Year1 - 1) * 12 + (12 - Month1) + Month2 + 1 : Month2 - Month1 + 1);
Example:
Year1/Month1: 2021/10 Year2/Month2: 2022/08 numberOfMonths = 11;
Or same year:
Year1/Month1: 2021/10 Year2/Month2: 2021/12 numberOfMonths = 3;
If you just want to touch one of them remove both + 1.