如何在Python中获得下个月的第一个日期?例如,如果现在是2019-12-31,下个月的第一天就是2020-01-01。如果现在是2019-08-01,下个月的第一天就是2019-09-01。在
我想到了这个:import datetime
def first_day_of_next_month(dt):
'''Get the first day of the next month. Preserves the timezone.
Args:
dt (datetime.datetime): The current datetime
Returns:
datetime.datetime: The first day of the next month at 00:00:00.
'''
if dt.month == 12:
return datetime.datetime(year=dt.year+1,
month=1,
day=1,
tzinfo=dt.tzinfo)
else:
return datetime.datetime(year=dt.year,
month=dt.month+1,
day=1,
tzinfo=dt.tzinfo)
对吗?有更好的方法吗?在