您好,登錄后才能下訂單哦!
這篇文章主要介紹了python如何獲取毫秒級時間的相關(guān)知識,內(nèi)容詳細易懂,操作簡單快捷,具有一定借鑒價值,相信大家閱讀完這篇python如何獲取毫秒級時間文章都會有所收獲,下面我們一起來看看吧。
import datetime
print(datetime.datetime.now().microsecond)
但通過以下代碼測試,發(fā)現(xiàn)返回的并不是ms的值,而是us的值:
import datetime
def getTime_ms():
return (datetime.datetime.now().hour*3600 +
datetime.datetime.now().minute*60+
datetime.datetime.now().second )*1000+
datetime.datetime.now().microsecond/1000
def timetime():
t = getTime_ms()
while getTime_ms() - t < 500:
pass
a = getTime_ms()
print(t, a, a- t)
if __name__ == '__main__':
from timeit import timeit
t = timeit('timetime()', 'from __main__ import timetime', number=1)
print(t)
打印結(jié)果
(35460614, 35461114, 500) 0.499531984329
def timetime():
t1 = datetime.datetime.now()
while 1:
t2 = datetime.datetime.now()
if (t2 - t1).microseconds >= 1:
print(t2,t2.microsecond,t1,t1.microsecond,(t2-t1).seconds)
break
if __name__ == '__main__':
from timeit import timeit
t = timeit('timetime()', 'from __main__ import timetime', number=1)
print(t)
#print (datetime.datetime.now())
輸出
(datetime.datetime(2019, 4, 3, 10, 6, 20, 461882), 461882, datetime.datetime(2019, 4, 3, 10, 6, 20, 461863), 461863, 0) 7.10487365723e-05
def timetime():
t1 = datetime.datetime.now()
while 1:
t2 = datetime.datetime.now()
if (t2 - t1).microseconds >= 1000:
print(t2,t2.microsecond,t1,t1.microsecond,(t2-t1).seconds)
break
if __name__ == '__main__':
from timeit import timeit
t = timeit('timetime()', 'from __main__ import timetime', number=1)
print(t)
#print (datetime.datetime.now())
輸出
(datetime.datetime(2019, 4, 3, 10, 13, 52, 76548), 76548, datetime.datetime(2019, 4, 3, 10, 13, 52, 75547), 75547, 0) 0.00104904174805
補充:Python datetime去除毫秒 返回當前時間
import datetime
if __name__ == '__main__':
a = datetime.datetime.now().replace(microsecond=0)
print(a, type(a))
運算結(jié)果:
2020-05-22 12:13:42 <class 'datetime.datetime'> Process finished with exit code 0
關(guān)于“python如何獲取毫秒級時間”這篇文章的內(nèi)容就介紹到這里,感謝各位的閱讀!相信大家對“python如何獲取毫秒級時間”知識都有一定的了解,大家如果還想學習更多知識,歡迎關(guān)注億速云行業(yè)資訊頻道。
免責聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。