Python 计算时间间隔

2023-12-25 17:21:20


from datetime import datetime


# 开始时间
starttime = datetime.now()

import time
time.sleep(5)

# 结束时间
endtime = datetime.now()

time_span = endtime - starttime
print(type(time_span))

print('时间间隔:', time_span.days)
print('时间间隔:', time_span.seconds)

'''
运行结果:
<class 'datetime.timedelta'>
运行时间: 0
运行时间: 5
'''

'''
timedelta中支持的参数:
days: float = ...,
seconds: float = ...,
microseconds: float = ...,?
milliseconds: float = ...,?
minutes: float = ...,
hours: float = ...,
weeks: float = ...,
'''

'''
参考:
https://blog.csdn.net/Datapad/article/details/114930222
'''

文章来源:https://blog.csdn.net/yudiandian2014/article/details/135201960
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。