def next(date):
"""Add one month to the given date.
Args:
date (datetime.date): The starting date.
Returns:
datetime.date: One month after the starting date unless the starting
date falls on a day that is not in the next month; in that case, it
returns the last day of the next month.
Example:
>>> import datetime
>>> next(datetime.date(2016, 1, 31))
datetime.date(2016, 2, 29)
"""
year, month = (date.year, date.month + 1) if date.month < 12 else (date.year + 1, 1)
_, length = calendar.monthrange(year, month)
day = min(length, date.day)
return datetime.date(year, month, day)
评论列表
文章目录