如何使用python转换此日期时间?
2017-10-16T08:27:16+0000
我尝试使用strptime但得到ValueError:时间数据'2017-10-16T08:27:16 + 0000'与格式'%d%B%Y不匹配。 %H:%M'
import datetime
datetime.datetime.strptime("2017-10-16T08:27:16+0000", "The %d %B %Y at. %H:%M")
'''
I want my output to look like this
The 16 october 2017 at. 08:27
'''
答案 0 :(得分:2)
首先正确解析字符串,然后以所需格式打印:
import datetime
date = datetime.datetime.strptime("2017-10-16T08:27:16+0000", "%Y-%m-%dT%H:%M:%S%z")
print(date.strftime("The %d %B %Y at. %H:%M"))
答案 1 :(得分:1)
https://blogs.harvard.edu/rprasad/2011/09/21/python-string-to-a-datetime-object/
您必须先使用strptime()
删除日期,然后使用strftime()
import datetime
time = "2017-10-16T08:27:16+0000"
stripedTime = datetime.datetime.strptime(time, '%Y-%m-%dT%I:%M:%S%z')
rebuildTime = stripedTime.strftime('The %d %B %Y at. %H:%M')
print(rebuildTime)