如何获取开始时间和结束时间范围之间的所有时间集?

发布于 2025-01-18 10:13:37 字数 522 浏览 0 评论 0 原文

我想在Python中获取开始时间和结束时间之间的所有时间,所以我使用带有范围函数的for循环。

有 2 个变量,ab,其时间格式为 %H:%M:%S

这两个变量是开始时间和结束时间,我想打印开始时间和结束时间之间的所有时间。

import datetime
from datetime import datetime
import time

a = '20:15:16'
b = '20:32:55'

a = datetime.strptime(a,'%H:%M:%S').time()
b = datetime.strptime(b,'%H:%M:%S').time()

for i in range(a,b):
    print(i)

为此,我收到错误 - datetime.time' 对象无法解释为整数

我想打印 ab 之间的所有时间。

I want to get all the time between start time and end time in python, so I was using for loop with range function.

There are 2 variables, a and b which have time in %H:%M:%S format.

These 2 variables are start and end time and I want to print all the time between the start and end time.

import datetime
from datetime import datetime
import time

a = '20:15:16'
b = '20:32:55'

a = datetime.strptime(a,'%H:%M:%S').time()
b = datetime.strptime(b,'%H:%M:%S').time()

for i in range(a,b):
    print(i)

For this I am getting an error - datetime.time' object cannot be interpreted as an integer.

I want to print all the time between a and b.

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

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

发布评论

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

评论(1

情独悲 2025-01-25 10:13:37

两次之间有无限的时刻。我认为您可能会问:“如何在A和B之间每秒或每一分钟打印一个时间戳?”

我认为您不想使用范围功能。您看到的错误是因为Range期望整数是输入,而不是整个DateTime对象。

这是可以做您想要的事情:

from datetime import datetime, timedelta

# Define our times
str_A = '20:15:16'
str_B = '20:32:55'

# Create our datetime objects
A = datetime.strptime(str_A,'%H:%M:%S')
B = datetime.strptime(str_B,'%H:%M:%S')

# Create a loop where you print a time, starting with time A
# and then increase the time stamp by some value, in this case,
# 1 minute, until you reach time B
tmp = a
while tmp <= b:
    print(tmp.time())
    tmp = tmp + timedelta(minutes=1)

请注意,

print(tmp.time())

我们只在需要时才提取时间零件,将对象作为DateTime对象进行轻松操作。

我使用此问题供参考:
什么是标准将n秒添加到DateTime.Time中的方法?

因此,这个问题真的很可爱。阅读有一些东西,“我需要在这两次之间打印“一直”,这给我带来了欢乐。

There are infinite moments between two times. I think you might be asking, "How can I print a timestamp for every second or every minute between A and B?"

I don't think you want to be using the range function. The error you are seeing is because range expects integers as input, not whole datetime objects.

Here is something that may do what you want:

from datetime import datetime, timedelta

# Define our times
str_A = '20:15:16'
str_B = '20:32:55'

# Create our datetime objects
A = datetime.strptime(str_A,'%H:%M:%S')
B = datetime.strptime(str_B,'%H:%M:%S')

# Create a loop where you print a time, starting with time A
# and then increase the time stamp by some value, in this case,
# 1 minute, until you reach time B
tmp = a
while tmp <= b:
    print(tmp.time())
    tmp = tmp + timedelta(minutes=1)

Please notice the line,

print(tmp.time())

where we only extract the time part when we need it, leaving the object as a datetime object for easy manipulation.

I used this question for reference:
What is the standard way to add N seconds to datetime.time in Python?

So this question is really adorable. There is something about reading, 'I need to print "all the time" between these two times' that gives me joy.

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