Created
November 5, 2018 08:41
-
-
Save RRRoger/aa5fe58c4b7d90ae9e8eeafcc1fcd634 to your computer and use it in GitHub Desktop.
月加减
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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