如何将时间戳字符串转换为日期时间对象? [英] How to convert timestamp string to datetime object?
本文介绍了如何将时间戳字符串转换为日期时间对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能的重复:
在 Python 中将 unix 时间戳字符串转换为可读日期
我有一个时间戳:
t = 1322745926.123
如何将时间戳转换为日期时间对象?
How to convert timestamp to datetime object ?
datetime.strptime(t,date_format)
上述函数调用中的date_format
应该是什么?
What should date_format
be in the above function call?
推荐答案
datetime.strptime() 不是解决您问题的正确函数.它将像30 Nov 00"这样的字符串转换为 struct_time 对象.
datetime.strptime() is not the right function for your problem. It convertes a string like "30 Nov 00" to a struct_time object.
你可能想要
from datetime import datetime
t = 1322745926.123
datetime.fromtimestamp(t).isoformat()
这段代码的结果是
'2011-12-01T14:25:26.123000'
如果您的时间码是一个字符串,您可以这样做:
if your timecode is a string you can do this:
from datetime import datetime
t = "1322745926.123"
datetime.fromtimestamp(float(t)).isoformat()
这篇关于如何将时间戳字符串转换为日期时间对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文