import calendar


def month_delta(date, months):
    """Add or subtract months from date."""
    day = date.day
    # subtract one because months are not zero-based
    month = date.month + months - 1
    year = date.year + month // 12
    # now add it back
    month = month % 12 + 1
    days_in_month = calendar.monthrange(year, month)[1]
    if day >= days_in_month:
        day = days_in_month
    try:
        return date.replace(year, month, day)
    except ValueError:
        raise OverflowError('date value out of range')