我想在8:00到17:00之间每15分钟循环一次。

8 投票
6 回答
5123 浏览
提问于 2025-04-17 15:18

我想要在两个时间之间循环,从早上8点到下午5点,每15分钟一次。

我希望得到的结果是一个时间列表,比如:

[8:00, 8:15, 8:30, 8:45, 9:00]

这是我目前做的:

now = datetime(2013, 2, 9, 8, 00)
end = now + timedelta(hours=9)

但是我还不知道怎么运行这个循环,才能得到我想要的列表。

谢谢你的帮助。

6 个回答

2
l=[]

while now<end:
    l.append(now)
    now+=timedelta(minutes=15)

当然可以!请把你想要翻译的内容发给我,我会帮你把它变得简单易懂。

3

这个可以正常运行:

import datetime

now = datetime.datetime(2013, 2, 9, 8, 00)
end=now+datetime.timedelta(hours=9)

l=[]
while now<=end:
    l.append(now)
    now+=datetime.timedelta(minutes=15)

print [t.strftime("%H:%M") for t in l]  

输出结果是:

['08:00', '08:15', '08:30', '08:45', '09:00', '09:15', '09:30', '09:45', '10:00', '10:15', '10:30', '10:45', '11:00', '11:15', '11:30', '11:45', '12:00', '12:15', '12:30', '12:45', '13:00', '13:15', '13:30', '13:45', '14:00', '14:15', '14:30', '14:45', '15:00', '15:15', '15:30', '15:45', '16:00', '16:15', '16:30', '16:45', '17:00']
6

你是说这个吗?

>>> now = datetime(2013,2,9,8,0)
>>> end = now + timedelta(hours=9)
>>> while now <= end:
        print 'doing something at', now
        now += timedelta(minutes=15)

doing something at 2013-02-09 08:00:00
doing something at 2013-02-09 08:15:00
doing something at 2013-02-09 08:30:00
doing something at 2013-02-09 08:45:00
../..

撰写回答