You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Python中如何对时分秒格式的时间值列表进行求和计算

计算登录总时长实现方案

实现思路

  • 把每个小时:分钟:秒格式的时间拆分为时、分、秒三个独立数值
  • 统一换算为秒单位累加得到总秒数,避免时分秒分别累加时需要额外处理进位的问题
  • 最后将总秒数转换回目标格式,分钟和秒不足两位时自动补前导零,保证和原格式一致

代码实现(Python)

timeList = ['1814:34:54', '205:30:52', '241:59:14', '1310:14:05', '2077:24:12', '1764:44:58', '125:29:49', '1857:19:52', '960:21:27', '1898:35:39', '1680:35:56', '1865:17:39', '34:10:32', '752:55:52', '1887:02:51', '861:54:07', '1927:17:37', '1356:00:31', '51:47:41', '1815:42:44', '1927:37:08', '1375:17:42', '2153:11:28', '805:40:50', '1071:11:56', '1605:12:47', '2032:19:39', '958:59:26', '98:33:42', '731:22:26', '755:02:55', '994:53:57', '264:35:51', '1973:50:07', '1805:00:41', '1554:52:17', '1942:38:34', '1147:16:29', '996:46:31', '1913:41:07', '244:46:29']

total_seconds = 0
for time_str in timeList:
    h, m, s = map(int, time_str.split(':'))
    total_seconds += h * 3600 + m * 60 + s

# 转换回时分秒格式
total_h = total_seconds // 3600
remain = total_seconds % 3600
total_m = remain // 60
total_s = remain % 60

# 格式化输出,分钟秒补前导零
total_time = f"{total_h}:{total_m:02d}:{total_s:02d}"
print(f"总登录时长:{total_time}")

运行结果

总登录时长:48614:26:58

如果需要换算为带天数的格式,可在上述代码基础上补充如下逻辑:

days = total_h // 24
final_h = total_h % 24
print(f"总登录时长:{days}天 {final_h}:{total_m:02d}:{total_s:02d}")
# 输出结果:总登录时长:2025天 14:26:58

内容的提问来源于stack exchange,提问作者David Kurtenbach

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.05 21:36:03