Python中的日期时间当前年和月 [英] Datetime current year and month in Python

查看:338
本文介绍了Python中的日期时间当前年和月的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我必须具有日期时间的当前年份和月份。

I must have the current year and month in datetime.

我使用以下方法:

datem = datetime.today().strftime("%Y-%m")
datem = datetime.strptime(datem, "%Y-%m")

还有其他方法吗?

推荐答案

使用:

from datetime import datetime
today = datetime.today()
datem = datetime(today.year, today.month, 1)

我假设您想要第一个月。

I assume you want the first of the month.

这篇关于Python中的日期时间当前年和月的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆