编程语言
首页 > 编程语言> > python – 将unix时间戳字符串转换为可读日期

python – 将unix时间戳字符串转换为可读日期

作者:互联网

我在Python中有一个表示unix时间戳(即“1284101485”)的字符串,我想将其转换为可读日期.当我使用time.strftime时,我得到一个TypeError:

>>>import time
>>>print time.strftime("%B %d %Y", "1284101485")

Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: argument must be 9-item sequence, not str

解决方法:

使用datetime模块:

from datetime import datetime
ts = int("1284101485")

# if you encounter a "year is out of range" error the timestamp
# may be in milliseconds, try `ts /= 1000` in that case
print(datetime.utcfromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S'))

标签:strftime,python,datetime,unix-timestamp
来源: https://codeday.me/bug/20190911/1803696.html