Python 獲取秒級時間戳與毫秒級時間戳


原文:Python獲取秒級時間戳與毫秒級時間戳

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'))   #日期格式化
復制代碼

返回

1499825149.26
1499825149
1499825149257
1499825149257
2017-07-12 10:05:49

 

2、將日期轉為秒級時間戳

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

返回

1514774430

 

3、將秒級時間戳轉為日期

ts = 1515774430
dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts))
print(dt)

返回

2018-01-13 00:27:10


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM