I try to loop between two times, from 8:00 to 17:00 every 15 minutes

I try to loop between two points, from 8:00 to 17:00 every 15 minutes

The expected output will be a list like

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

It is still I got

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

But I can’t figure out how to start the loop to return the list I want.

Thanks for watching.

+5
source share
6 answers

Do 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
../..
+3
source

It 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']
+3
source
l=[]

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

, :

:

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)]

:

['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']
+2
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)

, .

+1

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

, .

+1

All Articles