python3 秒转时分秒

code

import datetime
import time
import os

start_run_time="2020-12-11 15:16:08"

print(start_run_time)
#开始时间转为秒级时间戳
ts1=time.mktime(time.strptime(start_run_time, '%Y-%m-%d %H:%M:%S'))

#当前时间
t2=datetime.datetime.now()
#当前日期
ct2 =t2.strftime('%Y-%m-%d %H:%M:%S')
#转为秒级时间戳
ts2=time.mktime(time.strptime(ct2, '%Y-%m-%d %H:%M:%S'))

#计算持续时间
diff_time=(datetime.datetime.fromtimestamp(ts2)-datetime.datetime.fromtimestamp(ts1)).seconds
m, s = divmod(diff_time, 60)
h, m = divmod(m, 60)
res_diff_time="{:02}:{:02}:{:02}".format(h, m, s)
print(res_diff_time)

原文地址:https://www.cnblogs.com/sea-stream/p/14120856.html