美文网首页
python 获得毫秒级时间戳

python 获得毫秒级时间戳

作者: 王镇_ee87 | 来源:发表于2021-01-20 08:52 被阅读0次

python 时间戳

import time
import datetime

t = time.time()

>>> print (t)                       #原始时间数据
>>> print (int(t))                  #秒级时间戳
>>> print (int(round(t * 1000)))    #毫秒级时间戳

1499825149.26
1499825149
1499825149257

nowTime = lambda:int(round(t * 1000))
>>> print (nowTime());              #毫秒级时间戳,基于lambda
1499825149257
>>> print (datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S'))   #日期格式化
2017-07-12 10:05:49

将日期转为秒级时间戳

dt = '2018-01-01 10:40:30'
ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
>>> print (ts)
1514774430

将秒级时间戳转为日期

ts = 1515774430
dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts))
>>> print (dt)
2018-01-13 00:27:10

相关文章

网友评论

      本文标题:python 获得毫秒级时间戳

      本文链接:https://www.haomeiwen.com/subject/ltabwktx.html