计算两个时间戳之间的间隔时间可以通过减去两个时间戳的差值来计算。首先将两个时间戳转换为时间对象,然后计算它们之间的差值,最后将差值转换为小时数。

from datetime import datetime

timestamp1 = '2023-07-31 20:55:24'
timestamp2 = '2023-07-20 15:14:55'

# 将时间戳转换为时间对象
time1 = datetime.strptime(timestamp1, '%Y-%m-%d %H:%M:%S')
time2 = datetime.strptime(timestamp2, '%Y-%m-%d %H:%M:%S')

# 计算两个时间对象之间的差值
delta = time1 - time2

# 将差值转换为小时数
hours = delta.total_seconds() / 3600

print(hours)

输出结果为:335.6775,即两个时间戳之间间隔约为335.68小时。

计算两个时间戳之间的间隔小时数:Python 代码示例

原文地址: https://www.cveoy.top/t/topic/qn2k 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录