def month_add(date, months):
    """Add number of months to date.

    >>> import datetime
    >>> date = datetime.date(1986, 3, 9)
    >>> month_add(date, -12)
    datetime.date(1985, 3, 9)
    >>> month_add(date, -1)
    datetime.date(1986, 2, 9)
    >>> month_add(date, 0)
    datetime.date(1986, 3, 9)
    >>> month_add(date, 3)

    >>> date = datetime.datetime(1986, 3, 9)
    >>> month_add(date, 12)
    datetime.datetime(1987, 3, 9)
    """
    year, month = divmod(date.year * 12 + date.month + months, 12)
    return date.replace(year=year, month=month)