try:
    nextmonthdate = x.replace(month=x.month + 1)
except ValueError:
    if x.month == 12:
        nextmonthdate = x.replace(year=x.year + 1, month=1)
how to get the same day of next month of a given day in python using datetime