1、獲取秒級時間戳與毫秒級時間戳、微秒級時間戳
import time
import datetime
t = time.time()
print (t) #原始時間數據
print (int(t)) #秒級時間戳
print (int(round(t * 1000))) #毫秒級時間戳
print (int(round(t * 1000000))) #微秒級時間戳
返回
1499825149.257892 #原始時間數據
1499825149 #秒級時間戳,10位
1499825149257 #毫秒級時間戳,13位
1499825149257892 #微秒級時間戳,16位
2、獲取當前日期時間
dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
dt_ms = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') # 含微秒的日期時間,來源 比特量化
print(dt)
print(dt_ms)
返回
2018-09-06 21:54:46
2018-09-06 21:54:46.205213
3、將日期轉為秒級時間戳
dt = '2018-01-01 10:40:30'
ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
print (ts)
返回
1514774430
4、將秒級時間戳轉為日期
ts = 1515774430
dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts))
print(dt)
返回
2018-01-13 00:27:10
5、時間格式轉成另一種時間格式
dt = '08/02/2019 01:00'
dt_new = datetime.datetime.strptime(dt, '%m/%d/%Y %H:%M').strftime('%Y-%m-%d %H:%M:%S')
print(dt_new)
返回
2019-08-02 01:00:00
6、轉結構體時間struct_time
ta_dt = time.strptime("2018-09-06 21:54:46", '%Y-%m-%d %H:%M:%S') #日期時間轉結構體
ta_ms = time.localtime(1486188476) #時間戳轉結構體,注意時間戳要求為int,來源 比特量化
print(ta_dt)
print(ta_ms)
返回
time.struct_time(tm_year=2018, tm_mon=9, tm_mday=6, tm_hour=21, tm_min=54, tm_sec=46, tm_wday=3, tm_yday=249, tm_isdst=-1)
time.struct_time(tm_year=2017, tm_mon=2, tm_mday=4, tm_hour=14, tm_min=7, tm_sec=56, tm_wday=5, tm_yday=35, tm_isdst=0)
以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持我們。
時間: 2019-09-24