def add_months_to_date(months, date):
month = date.month<EOL>new_month = month + months<EOL>years = <NUM_LIT:0><EOL>while new_month < <NUM_LIT:1>:<EOL><INDENT>new_month += <NUM_LIT:12><EOL>years -= <NUM_LIT:1><EOL><DEDENT>while new_month > <NUM_LIT:12>:<EOL><INDENT>new_month -= <NUM_LIT:12><EOL>years += <NUM_LIT:1><EOL><DEDENT>year = date.year + years<EOL>try:<EOL><INDENT>return datetime.date(year, new_month, date.day)<EOL><DEDENT>except ValueError:<EOL><INDENT>if months > <NUM_LIT:0>:<EOL><INDENT>new_month += <NUM_LIT:1><EOL>if new_month > <NUM_LIT:12>:<EOL><INDENT>new_month -= <NUM_LIT:12><EOL>year += <NUM_LIT:1><EOL><DEDENT>return datetime.datetime(year, new_month, <NUM_LIT:1>)<EOL><DEDENT>else:<EOL><INDENT>new_day = calendar.monthrange(year, new_month)[<NUM_LIT:1>]<EOL>return datetime.datetime(year, new_month, new_day)<EOL><DEDENT><DEDENT>
Add a number of months to a date