如何在Python中添加小时数?格式如下:

发布于 2025-01-13 17:06:01 字数 145 浏览 1 评论 0原文

如何在Python中添加小时数?采用以下格式:

hour1= '20:00'
hour2= '25:00' 
sum= hour1 + hour2

因此正确的打印和格式如我想要的那样: 总和结果 45:00:00

how to add hours in python? in the format below:

hour1= '20:00'
hour2= '25:00' 
sum= hour1 + hour2

so the correct print and formatted as I want it would be:
result of the sum 45:00:00

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

可是我不能没有你 2025-01-20 17:06:01

我刚刚复制并编辑了这个答案

import datetime

# for show hours use below format
timeList = ['25:00:00', '20:00:00']
mysum = datetime.timedelta()
for i in timeList:
    (h, m, s) = i.split(':')
    d = datetime.timedelta(hours=int(h), minutes=int(m), seconds=int(s))
    mysum += d
print(str(mysum))

I just copied and edited this answer:

import datetime

# for show hours use below format
timeList = ['25:00:00', '20:00:00']
mysum = datetime.timedelta()
for i in timeList:
    (h, m, s) = i.split(':')
    d = datetime.timedelta(hours=int(h), minutes=int(m), seconds=int(s))
    mysum += d
print(str(mysum))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文