Skip to content

Instantly share code, notes, and snippets.

@ei-grad
Created June 21, 2013 13:09
Show Gist options
  • Select an option

  • Save ei-grad/5831026 to your computer and use it in GitHub Desktop.

Select an option

Save ei-grad/5831026 to your computer and use it in GitHub Desktop.
from datetime import datetime, date
import calendar
def monthdelta(dt0, months=1):
year = dt0.year + (dt0.month + months) // 12
month = 1 + (dt0.month + months - 1) % 12
maxday = calendar.monthrange(year, month)[1]
if maxday < dt0.day:
d = date(year, month, maxday)
else:
d = date(year, month, dt0.day)
return datetime.combine(date=d, time=dt0.time())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment