将未知时间戳转换为Python中的日期时间

2 投票
1 回答
1146 浏览
提问于 2025-04-16 06:29

我从WordPress的API得到了一个时间戳,但我通常用来把时间戳转换成可读日期的方法却不管用了。

我运行了这个代码:

print pages[1]['dateCreated']
print datetime.fromtimestamp(pages[1]['dateCreated'])

结果却是这个:

20100228T09:25:07
Traceback (most recent call last):
  File "C:\Python26\Lib\SITE-P~1\PYTHON~1\pywin\framework\scriptutils.py", line 325, in RunScript
    exec codeObject in __main__.__dict__
  File "C:\Documents and Settings\mmorisy\Desktop\My Dropbox\python\betterblogmaster3.py", line 28, in <module>
    print datetime.fromtimestamp(pages[1]['dateCreated'])
AttributeError: DateTime instance has no attribute '__float__'

有没有什么建议呢?

1 个回答

3

假设你已经使用了 from datetime import datetime 这行代码:

print datetime.strptime(pages[1]['dateCreated'],'%Y%m%dT%H:%M:%S')

撰写回答