python将时间戳转换为日期格式

简单来说,时间戳是当前时间距离 1970-01-01T00:00:00Z 有多少秒。
比如,当前时间为 2022年7月6日10点32分50秒,故这个时间点对应的时间戳为 1657074770

1.时间戳获取

import time
now=time.time()#获取当前时间时间戳

在这里插入图片描述

2. 毫秒级时间戳转换为日期

时间戳为1678723200000时

import time
now = int(1678723200000/1000)
timeArray = time.localtime(now)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print(otherStyleTime)

在这里插入图片描述

3.秒级时间戳转换

import time
now = time.time()
timeArray = time.localtime(now)
otherStyleTime = time.strftime("%Y-%m-%d %H:%M:%S", timeArray)
print(now)
print(otherStyleTime)

在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/qq_62975494/article/details/130118575