Here is an algorithm to calculate the number of elapsed months separating two dates. It accounts for the day of month and the fact that months have different lengths. It does not matter whether days and months are zero indexed.


Python implementation

date1 and date2 are objects.

import calendar

def calculate_monthdelta(date1, date2):
    def is_last_day_of_the_month(date):
        days_in_month = calendar.monthrange(date.year, date.month)[1]
        return == days_in_month
    imaginary_day_2 = 31 if is_last_day_of_the_month(date2) else
    monthdelta = (
        (date2.month - date1.month) +
        (date2.year - date1.year) * 12 +
        (-1 if > imaginary_day_2 else 0)
    return monthdelta