我是靠谱客的博主 称心世界,这篇文章主要介绍python 判断当前日期是月的第一天_如何在Python中获得下个月的第一天?,现在分享给大家,希望可以做个参考。

如何在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)

对吗?有更好的方法吗?在

最后

以上就是称心世界最近收集整理的关于python 判断当前日期是月的第一天_如何在Python中获得下个月的第一天?的全部内容,更多相关python内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(45)

评论列表共有 0 条评论

立即
投稿
返回
顶部