简体   繁体   English

我试图在两次之间循环,每隔15分钟从8:00到17:00

[英]I am trying to loop between two times, from 8:00 to 17:00 for every 15 mins

I am trying to loop between two times, from 8:00 to 17:00 for every 15 mins 我试图在两次之间循环,每隔15分钟从8:00到17:00

The expected output will be a list of times like 预期的输出将是一个时间列表

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

This is so far I got 这是迄今为止我得到的

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

But I can't figure out how to run the loop to return me my desired list. 但我无法弄清楚如何运行循环以返回我想要的列表。

Thanks for looking. 谢谢你的期待。

You mean this? 你是这个意思?

>>> 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
../..

This works: 这有效:

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]  

prints: 打印:

['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']
l=[]

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

If you can calculate ahead of time the number of elements in the list, you can use this: 如果您可以提前计算列表中元素的数量,则可以使用:

Input: 输入:

import datetime
now = datetime.datetime(2013, 2, 9, 8, 00)
print [(now + datetime.timedelta(minutes=15*n)).strftime('%H:%M') for n in range(37)]

Output: 输出:

['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']
import datetime as dt

def timerange (start, end, step):
    while start < end:
        yield start
        start += step

for x in timerange (dt.datetime (2013, 2, 9, 8), dt.datetime (2013, 2, 9, 17), dt.timedelta (minutes = 15) ):
        print (x)

In case you need stepped time ranges more frequently. 如果您需要更频繁地步进时间范围。

I would seriously give Delorean a serious look to do such thing would be easy as a for loop. 我会认真地让Delorean认真做这件事就像for循环一样容易。

>>> import delorean
>>> from delorean import stops
>>> for stop in stops(freq=delorean.MINUTELY, count=4, start=d1, interval=15):
...     print stop.datetime
... 
2012-05-06 00:00:00+00:00
2012-05-06 00:15:00+00:00
2012-05-06 00:30:00+00:00
2012-05-06 00:45:00+00:00

You can also provide a stop time which would be excellent example with your question. 您还可以提供一个停止时间,这将是您的问题的绝佳示例。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何获取y和z之间的每x分钟循环的python代码? - How do I get python code to loop every x mins between the times y and z? 为什么尝试使用 matplotlib 对它们进行 plot 时时间是 00:00? - Why times are 00:00 when trying to plot them using matplotlib? 如何从字符串中删除00:00:00 - How do I remove the 00:00:00 from my string 如何从python中的日期中删除 00:00:00 - how can I strip the 00:00:00 from a date in python 睡到下一个15分钟的小时间隔(00:00,00:15,00:00,00:45) - Sleep till next 15 minute hourly interval (00:00, 00:15, 00:30, 00:45) 如何阻止python pandas将“00:00:00”添加到每个日期? - How to stop python pandas from adding “00:00:00” to every date? 如何在整小时内激活 python 程序? (下午 12:00:00,上午 04:00:00) - How do I activate a python program at exact whole hours? (12:00:00pm, 04:00:00am) 无法在大熊猫的不规则15分钟时间戳00:14:59 - 00:29:59之间重新采样 - Unable to resample between irregular 15 minutes time-stamp 00:14:59 - 00:29:59 in pandas 如何从python中的“ Tue May 15 2018 00:00:00 GMT-0400(EDT)”格式中获取日期 - How to get date from this format “Tue May 15 2018 00:00:00 GMT-0400 (EDT)” in python Python datetime strptime解析错误,为什么在&#39;2008-10-26 01:00:00&#39;和&#39;2008-10-26 00:00:00&#39;之间需要两个小时? - Python datetime strptime parsing error, why is two hours between '2008-10-26 01:00:00' and '2008-10-26 00:00:00'?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM