首页 > 解决方案 > 将日期格式从 2019-02-04 更改为 2 月 4 日

问题描述

我有一个用于电子纸框架的 python 脚本。我想将日期从“2019-02-04”改为“04 feb”。我已经调整代码几个小时了,但我无法让它工作。

代码如下所示:

# Calendar strings to be displayed
day_str = time.strftime("%A")
day_number = time.strftime("%d")
month_str = time.strftime("%B") + ' ' + time.strftime("%Y")
month_cal = str(calendar.month(int(time.strftime("%Y")), int(time.strftime("%m"))))
month_cal = month_cal.split("\n",1)[1];
update_moment = time.strftime("%H") + ':' + time.strftime("%M")

标签: pythonraspberry-pi

解决方案


d = '2019-02-04'
datetime.strptime(d, '%Y-%m-%d').strftime('%d %b')

输出

'04 Feb'

推荐阅读