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

时间:2010-11-03 15:10:20

标签: python datetime timestamp

我从WordPress API获取时间戳,但converting from timestamp to datetime的常用方法让我失望。

我跑了这个:

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 个答案:

答案 0 :(得分:3)

假设你from datetime import datetime

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