在python中转换日期时间

2024-06-06 03:14:18 发布

您现在位置:Python中文网/ 问答频道 /正文

如何使用python转换这个datetime?你知道吗

2017-10-16T08:27:16+0000

我尝试使用strtime,但获取值错误:时间数据'2017-10-16T08:27:16+0000'与格式'at的%d%B%Y'不匹配。%小时:%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
'''

Tags: theto数据importoutputdatetimemy格式
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"))

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)

相关问题 更多 >