Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 480
mmrs151 Avatar asked Feb 09 '13 01:02

mmrs151


People also ask

How do you make a loop run a certain number of times?

To loop through a set of code a certain number of times, you can use the range() function, which returns a list of numbers starting from 0 to the specified end number. You haven't learned about functions yet, but you will soon!


1 Answers

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
../..
like image 172
isedev Avatar answered Nov 15 '22 09:11

isedev