python3 计算时间间隔

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

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))

 

 

你可能感兴趣的:(python3 计算时间间隔)