+2 votes
in Programming Languages by (11.2k points)
I want to calculate the future date using current date (or any other date) and number of months. I tried to use calendar, but didn't get the desired result.

1 Answer

0 votes
by (11.2k points)
edited by

You can calculate the future date using calendar. I tried the following code and it worked perfectly. 

from datetime import date
import calendar

def futureDate(date, delta):
    m, y = (date.month+delta) % 12, date.year + ((date.month)+delta-1) // 12
    if m==0:
        m = 12
    d = min(date.day, calendar.monthrange(y, m)[1])
    return date.replace(day=d,month=m, year=y)

#td = date.today()
td = date(2016,6,19)
nd= futureDate(td,5)
print nd

...