把時間戳進行轉換為正常時間格式,代碼如下:
import time
date=1584670171335
timeArray=time.localtime(temp)
otherStyleTime=time.strftime('%y--%m--%d %H:%M:%S',timeArray)
print(otherStyleTime)
報錯:
【原因】報錯原因在date的長度,一般爬取下來的時間戳長度都是13位的數字,而time.localtime的參數要的長度是10位,所以我們需要將其/1000並取整即可。
修改如下:
import time
temp=1584670171335
timeArray=time.localtime(int(temp/1000)) #修改后代碼
otherStyleTime=time.strftime('%y--%m--%d %H:%M:%S',timeArray)
print(otherStyleTime)
【附】
import time
#時間戳轉為正常時間顯示
temp=2219497200000
timeArray=time.localtime(int(temp/1000))
otherStyleTime=time.strftime('%Y-%m-%d %H:%M:%S',timeArray)
print(otherStyleTime)
#正常時間格式轉為時間戳
date='2020-05-01 23:00:00'
timeArray1=time.strptime(date,'%Y-%m-%d %H:%M:%S')
print(timeArray1)
dateStamp=int(time.mktime(timeArray1))#轉為的時間戳格式是10位
print(dateStamp)
