如何将时间戳字符串转换为日期时间对象?

问题描述:

可能的重复:
在 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()