python计算时间间隔

2023-01-31 02:01:51 时间 计算 间隔

计算两个时间点之间的时间间隔,可使用以下方法:

datetime模块

参考 https://docs.python.org/3/library/datetime.html

import datetime
starttime = datetime.datetime.now()
#long running
endtime = datetime.datetime.now()
duringtime = endtime - starttime
print duringtime.seconds

两者相减,得到的duringtime是一个timedelta的实例,这个实例自身有属性有

 days
 seconds
 microseconds

time.time( )模块

import time
a = time.time()
# short running
b = time.time()
seconds = b - a
m, s = divmod(seconds, 60)
h, m = divmod(m, 60)
print ("%02d:%02d:%02d" % (h, m, s))

参考:
Https://www.jianshu.com/p/03D6e9867fdf
https://blog.csdn.net/xiaoQL520/article/details/78435175

相关文章