1.获取秒级时间戳与毫秒级时间戳
import time import datetime t = time.time() print (t) #原始时间数据 print (int(t)) #秒级时间戳 print (int(round(t * 1000))) #毫秒级时间戳 nowTime = lambda:int(round(t * 1000)) print (nowTime()); #毫秒级时间戳,基于lambda print (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')) #日期格式化
运行结果
1640869590.0347595 1640869590 1640869590035 1640869590035 2021-12-30 21:06:30
2.将日期转为秒级时间戳
dt = '2021-12-30 21:00:00' ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S"))) print(ts)
运行结果
1640869200
3.将秒级时间戳转为日期
ts = 1640869200 dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts)) print(dt)
运行结果
2021-12-30 21:00:00