python3 计算时间间隔
程序员文章站
2024-01-05 14:48:46
...
计算两个时间点之间的时间间隔,可使用以下方法:
datetime模块
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))