python计算两个时间间隔

时间计算

import time

def get_time(a1, a2):
    timeArraya1 = time.strptime(a1, "%Y-%m-%d %H:%M:%S")
    # 转换为时间戳
    timeStampa1 = int(time.mktime(timeArraya1))

    timeArraya2 = time.strptime(a2, "%Y-%m-%d %H:%M:%S")
    # 转换为时间戳
    timeStampa2 = int(time.mktime(timeArraya2))

    x = timeStampa2 - timeStampa1
    print("本次用时", round(x / 3600, 2), "小时")


if __name__ == '__main__':
    start = "2022-11-30 08:50:16"
    end = "2022-12-01 08:03:49"
    get_time(start, end)

你可能感兴趣的:(python)