from datetime import timedelta
try:
next_month = (x.replace(day=28) + timedelta(days=7)).replace(day=x.day)
except ValueError: # assuming January 31 should return last day of February.
next_month = (x + timedelta(days=31)).replace(day=1) - timedelta(days=1)