这篇文章主要介绍了python如何获取毫秒级时间的相关知识,内容详细易懂,操作简单快捷,具有一定借鉴价值,相信大家阅读完这篇python如何获取毫秒级时间文章都会有所收获,下面我们一起来看看吧。
import datetime
print(datetime.datetime.now().microsecond)
但通过以下代码测试,发现返回的并不是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)
打印结果
(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))
运算结果:
2020-05-22 12:13:42 <class 'datetime.datetime'> Process finished with exit code 0
关于“python如何获取毫秒级时间”这篇文章的内容就介绍到这里,感谢各位的阅读!相信大家对“python如何获取毫秒级时间”知识都有一定的了解,大家如果还想学习更多知识,欢迎关注亿速云行业资讯频道。
免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。