如何计算Python的时间差?

发布于 2025-02-06 07:26:17 字数 44 浏览 1 评论 0原文

示例:

9:43-17:27-在那段时间之间经过了几小时?

Example:

9:43 - 17:27 - how many hours and minutes elapsed between those times ?

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

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

发布评论

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

评论(2

顾忌 2025-02-13 07:26:17

这是获取总分钟数的一种方法:

from datetime import datetime

s = '9:30 - 14:00 ; 14:30 - 16:30'

sum(((b-a).total_seconds()/60 for x in s.split(' ; ')
     for a,b in [list(map(lambda t: datetime.strptime(t, '%H:%M'), x.split(' - ')))]))

输出:390.0

Here is one approach to get the number of total minutes:

from datetime import datetime

s = '9:30 - 14:00 ; 14:30 - 16:30'

sum(((b-a).total_seconds()/60 for x in s.split(' ; ')
     for a,b in [list(map(lambda t: datetime.strptime(t, '%H:%M'), x.split(' - ')))]))

Output: 390.0

暖树树初阳… 2025-02-13 07:26:17

如果您知道时间段永远不会跨越午夜,那么您可以简单地按时间分开时间字符串。Split(“:”),并用小时和分钟自己进行数学。

但是,正确的解决方案是导入DateTime模块并计算时间码。

这个示例可以凝结。我故意将其冗长地说明,而不必确切地知道您如何获得投入:

from datetime import datetime

times = [
    "9:30",
    "14:00",
    "14:30",
    "16:30"
]

#Just using today's date to fill in the values with assumption all times are on the same day.
year = 2022
month = 6
day = 9

date_times = []

for time in times:
    split_time = time.split(":")
    hour = split_time[0]
    minutes = split_time[1]
    timestamp = datetime.datetime.today(year=year, month=month, day=day, hour=hour, min=minutes)
    date_times.append(timestamp)

total_seconds = 0

for i in range(1, len(date_times), 2):
    delta = date_times[i] - date_times[i-1]  # The timedelta object returned will have days, seconds, milliseconds
    total_seconds += delta.days * 86400 + delta.seconds

hours = total_seconds // 3600  # Integer division
minutes = round((total_seconds % 3600) / 60)  # Change depending on if you want to round to nearest, or always up or down.

If you know that the time periods will never span midnight, then you could simply split the time strings with time.split(":") and do the math yourself with the hours and minutes.

However, the correct solution would be to import the datetime module and calculate the timedelta.

This example could be condensed. I intentionally made it verbose without knowing exactly how you're getting your inputs:

from datetime import datetime

times = [
    "9:30",
    "14:00",
    "14:30",
    "16:30"
]

#Just using today's date to fill in the values with assumption all times are on the same day.
year = 2022
month = 6
day = 9

date_times = []

for time in times:
    split_time = time.split(":")
    hour = split_time[0]
    minutes = split_time[1]
    timestamp = datetime.datetime.today(year=year, month=month, day=day, hour=hour, min=minutes)
    date_times.append(timestamp)

total_seconds = 0

for i in range(1, len(date_times), 2):
    delta = date_times[i] - date_times[i-1]  # The timedelta object returned will have days, seconds, milliseconds
    total_seconds += delta.days * 86400 + delta.seconds

hours = total_seconds // 3600  # Integer division
minutes = round((total_seconds % 3600) / 60)  # Change depending on if you want to round to nearest, or always up or down.
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文