1、獲取秒級時(shí)間戳與毫秒級時(shí)間戳、微秒級時(shí)間戳
import time
import datetime
t = time.time()
print (t) #原始時(shí)間數(shù)據(jù)
print (int(t)) #秒級時(shí)間戳
print (int(round(t * 1000))) #毫秒級時(shí)間戳
print (int(round(t * 1000000))) #微秒級時(shí)間戳
1499825149.257892 #原始時(shí)間數(shù)據(jù)
1499825149 #秒級時(shí)間戳,10位
1499825149257 #毫秒級時(shí)間戳,13位
1499825149257892 #微秒級時(shí)間戳,16位
2、獲取當(dāng)前日期時(shí)間
dt = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S')
dt_ms = datetime.datetime.now().strftime('%Y-%m-%d %H:%M:%S.%f') # 含微秒的日期時(shí)間,來源 比特量化
print(dt)
print(dt_ms)
2018-09-06 21:54:46
2018-09-06 21:54:46.205213
3、將日期轉(zhuǎn)為秒級時(shí)間戳
dt = '2018-01-01 10:40:30'
ts = int(time.mktime(time.strptime(dt, "%Y-%m-%d %H:%M:%S")))
print (ts)
1514774430
4、將秒級時(shí)間戳轉(zhuǎn)為日期
ts = 1515774430
dt = time.strftime("%Y-%m-%d %H:%M:%S", time.localtime(ts))
print(dt)
2018-01-13 00:27:10
5、時(shí)間格式轉(zhuǎn)成另一種時(shí)間格式
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、轉(zhuǎn)結(jié)構(gòu)體時(shí)間struct_time
ta_dt = time.strptime("2018-09-06 21:54:46", '%Y-%m-%d %H:%M:%S') #日期時(shí)間轉(zhuǎn)結(jié)構(gòu)體
ta_ms = time.localtime(1486188476) #時(shí)間戳轉(zhuǎn)結(jié)構(gòu)體,注意時(shí)間戳要求為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)
到此這篇關(guān)于Python獲取秒級時(shí)間戳與毫秒級時(shí)間戳的實(shí)現(xiàn)示例的文章就介紹到這了,更多相關(guān)Python 秒級時(shí)間戳與毫秒級時(shí)間戳內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
您可能感興趣的文章:- Python時(shí)間戳與時(shí)間字符串互相轉(zhuǎn)換實(shí)例代碼
- python正常時(shí)間和unix時(shí)間戳相互轉(zhuǎn)換的方法
- Python之time模塊的時(shí)間戳,時(shí)間字符串格式化與轉(zhuǎn)換方法(13位時(shí)間戳)
- Python3日期與時(shí)間戳轉(zhuǎn)換的幾種方法詳解
- python 獲取當(dāng)天凌晨零點(diǎn)的時(shí)間戳方法
- python 時(shí)間戳與格式化時(shí)間的轉(zhuǎn)化實(shí)現(xiàn)代碼
- python生成13位或16位時(shí)間戳以及反向解析時(shí)間戳的實(shí)例
- python utc datetime轉(zhuǎn)換為時(shí)間戳的方法
- python 獲取當(dāng)天每個(gè)準(zhǔn)點(diǎn)時(shí)間戳的實(shí)例