python时间戳转化时OSError: [Errno 22] Invalid argument
把时间戳进行转换为正常时间格式,代码如下:
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)

浙公网安备 33010602011771号