Python獲取秒級時間戳與毫秒級時間戳的示例代碼
1、獲取秒級時間戳與毫秒級時間戳、微秒級時間戳
import timeimport 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:462018-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 = 1515774430dt = 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)
到此這篇關于Python獲取秒級時間戳與毫秒級時間戳的實現示例的文章就介紹到這了,更多相關Python 秒級時間戳與毫秒級時間戳內容請搜索好吧啦網以前的文章或繼續瀏覽下面的相關文章希望大家以后多多支持好吧啦網!
相關文章:
