将UTC时间转换为python datetime [英] Convert UTC time to python datetime
本文介绍了将UTC时间转换为python datetime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有许多采用以下格式的UTC时间戳:
2012-04-30T23:08:56+00:00
我想将它们转换为python datetime对象,但是遇到了麻烦.
I have numerous UTC time stamps in the following format:
2012-04-30T23:08:56+00:00
I want to convert them to python datetime objects but am having trouble.
我的代码:
for time in data:
pythondata[i]=datetime.strptime(time,"%y-%m-%dT%H:%M:%S+00:00")
我收到以下错误:
ValueError: time data '2012-03-01T00:05:55+00:00' does not match format '%y-%m-%dT%H:%M:%S+00:00'
看起来我的格式正确,那为什么不起作用?
It looks like I have the proper format, so why doesn't this work?
推荐答案
将时间格式字符串中的年份标记更改为%Y
:
Change the year marker in your time format string to %Y
:
time = '2012-03-01T00:05:55+00:00'
datetime.strptime(time, "%Y-%m-%dT%H:%M:%S+00:00")
# => datetime.datetime(2012, 3, 1, 0, 5, 55)
See strftime()
and strptime()
behavior.
这篇关于将UTC时间转换为python datetime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文