Skip to content

Instantly share code, notes, and snippets.

@RRRoger
Created November 5, 2018 08:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save RRRoger/aa5fe58c4b7d90ae9e8eeafcc1fcd634 to your computer and use it in GitHub Desktop.
Save RRRoger/aa5fe58c4b7d90ae9e8eeafcc1fcd634 to your computer and use it in GitHub Desktop.
月加减
def add_months(dt, months):
"""
月加减
:param dt: datetime 实例
:param months: 加多少月
:return:
"""
month = dt.month - 1 + months
year = dt.year + month / 12
month = month % 12 + 1
day = min(dt.day, calendar.monthrange(year, month)[1])
return dt.replace(year=year, month=month, day=day)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment