>>> start_date = date(1983, 11, 23)
>>> start_date.replace(month=start_date.month+1)
datetime.date(1983, 12, 23)
这个工作直到月份是 <=11
, 只要我做
>>> start_date = date(1983, 12, 23)
>>> start_date.replace(month=start_date.month+1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: month must be in 1..12
当新月份添加到 12 月时,如何继续添加月份以增加年份?
最佳答案
dateutil库对于这样的计算很有用:
>>> start_date + relativedelta(months=2)
datetime.date(1984, 1, 23)
关于 python : How to add month to December 2012 and get January 2013?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12736229/