Efficient way of making time increment strings?

Like @juanpa.arrivillaga's answer, but with formatting:

from datetime import datetime, timedelta
t = datetime(1, 1, 1, hour=8, minute=0)
l = []
while t < datetime(1, 1, 1, hour=22, minute=1):
    l.append(t.strftime('%I:%M%p') + " - " + (t+timedelta(minutes=15)).strftime('%I:%M%p'))
    t += timedelta(minutes=15)

print(l)
>>>
['08:00AM - 08:15AM', '08:15AM - 08:30AM', '08:30AM - 08:45AM', '08:45AM - 09:00AM', '09:00AM - 09:15AM', '09:15AM - 09:30AM', '09:30AM - 09:45AM', '09:45AM - 10:00AM', '10:00AM - 10:15AM', '10:15AM - 10:30AM', '10:30AM - 10:45AM', '10:45AM - 11:00AM', '11:00AM - 11:15AM', '11:15AM - 11:30AM', '11:30AM - 11:45AM', '11:45AM - 12:00PM', '12:00PM - 12:15PM', '12:15PM - 12:30PM', '12:30PM - 12:45PM', '12:45PM - 01:00PM', '13:00PM - 01:15PM', '13:15PM - 01:30PM', '13:30PM - 01:45PM', '13:45PM - 02:00PM', '14:00PM - 02:15PM', '14:15PM - 02:30PM', '14:30PM - 02:45PM', '14:45PM - 03:00PM', '15:00PM - 03:15PM', '15:15PM - 03:30PM', '15:30PM - 03:45PM', '15:45PM - 04:00PM', '16:00PM - 04:15PM', '16:15PM - 04:30PM', '16:30PM - 04:45PM', '16:45PM - 05:00PM', '17:00PM - 05:15PM', '17:15PM - 05:30PM', '17:30PM - 05:45PM', '17:45PM - 06:00PM', '18:00PM - 06:15PM', '18:15PM - 06:30PM', '18:30PM - 06:45PM', '18:45PM - 07:00PM', '19:00PM - 07:15PM', '19:15PM - 07:30PM', '19:30PM - 07:45PM', '19:45PM - 08:00PM', '20:00PM - 08:15PM', '20:15PM - 08:30PM', '20:30PM - 08:45PM', '20:45PM - 09:00PM', '21:00PM - 09:15PM', '21:15PM - 09:30PM', '21:30PM - 09:45PM', '21:45PM - 10:00PM', '22:00PM - 10:15PM']

If you can use pandas, here's a shorter answer:

import pandas as pd

dr = pd.date_range('2021-01-01 08:00', '2021-01-01 22:00', freq='15min')
l = [t.strftime('%I:%M%p') + " - " + (t+pd.to_timedelta('15min')).strftime('%I:%M%p') for t in dr]

Edit: Edited the code according to @Shash Sinha's comment below.


Use the datetime module to work with dates and times...

>>> import datetime
>>> start = datetime.datetime(2021, 1, 24, 8)
>>> stop = datetime.datetime(2021, 1, 24, 22)
>>> fifteen_minutes = datetime.timedelta(minutes=15)
>>> current = start
>>> result = []
>>> while current <= stop:
...     result.append(current.time())
...     current += fifteen_minutes
...

This populates your list with datetime.date objects, although, note that I used datetime.datetime objects so that I was able to increment them with datetime.timedelta objects.

You probably want to stick with one of those, but you can always format the output, e.g:

>>> [t.strftime("%I%:%M%p") for t in result]
['08:00AM', '08:15AM', '08:30AM', '08:45AM', '09:00AM', '09:15AM', '09:30AM', '09:45AM', '10:00AM', '10:15AM', '10:30AM', '10:45AM', '11:00AM', '11:15AM', '11:30AM', '11:45AM', '12:00PM', '12:15PM', '12:30PM', '12:45PM', '01:00PM', '01:15PM', '01:30PM', '01:45PM', '02:00PM', '02:15PM', '02:30PM', '02:45PM', '03:00PM', '03:15PM', '03:30PM', '03:45PM', '04:00PM', '04:15PM', '04:30PM', '04:45PM', '05:00PM', '05:15PM', '05:30PM', '05:45PM', '06:00PM', '06:15PM', '06:30PM', '06:45PM', '07:00PM', '07:15PM', '07:30PM', '07:45PM', '08:00PM', '08:15PM', '08:30PM', '08:45PM', '09:00PM', '09:15PM', '09:30PM', '09:45PM', '10:00PM']

EDIT: Notice you wanted intervals. You can do something like:

list(zip(result, result[1:]))

to get every pair, then format accordingly.

So, something to the effect of:

>>> [f"{t1.strftime('%I:%M%p')} - {t2.strftime('%I:%M%p')}" for t1, t2 in zip(result, result[1:])]
['08:00AM - 08:15AM', '08:15AM - 08:30AM', '08:30AM - 08:45AM', '08:45AM - 09:00AM', '09:00AM - 09:15AM', '09:15AM - 09:30AM', '09:30AM - 09:45AM', '09:45AM - 10:00AM', '10:00AM - 10:15AM', '10:15AM - 10:30AM', '10:30AM - 10:45AM', '10:45AM - 11:00AM', '11:00AM - 11:15AM', '11:15AM - 11:30AM', '11:30AM - 11:45AM', '11:45AM - 12:00PM', '12:00PM - 12:15PM', '12:15PM - 12:30PM', '12:30PM - 12:45PM', '12:45PM - 01:00PM', '01:00PM - 01:15PM', '01:15PM - 01:30PM', '01:30PM - 01:45PM', '01:45PM - 02:00PM', '02:00PM - 02:15PM', '02:15PM - 02:30PM', '02:30PM - 02:45PM', '02:45PM - 03:00PM', '03:00PM - 03:15PM', '03:15PM - 03:30PM', '03:30PM - 03:45PM', '03:45PM - 04:00PM', '04:00PM - 04:15PM', '04:15PM - 04:30PM', '04:30PM - 04:45PM', '04:45PM - 05:00PM', '05:00PM - 05:15PM', '05:15PM - 05:30PM', '05:30PM - 05:45PM', '05:45PM - 06:00PM', '06:00PM - 06:15PM', '06:15PM - 06:30PM', '06:30PM - 06:45PM', '06:45PM - 07:00PM', '07:00PM - 07:15PM', '07:15PM - 07:30PM', '07:30PM - 07:45PM', '07:45PM - 08:00PM', '08:00PM - 08:15PM', '08:15PM - 08:30PM', '08:30PM - 08:45PM', '08:45PM - 09:00PM', '09:00PM - 09:15PM', '09:15PM - 09:30PM', '09:30PM - 09:45PM', '09:45PM - 10:00PM']

Here is a different solution using list comprehension:

ampm = ['am', 'pm']
hours = [8, 9, 10]
minutes = ['00', 15, 30, 45]

print([f'{y}:{z}{x}' for x in ampm for y in hours for z in minutes])

Output:

['8:00am', '8:15am', '8:30am', '8:45am', '9:00am', '9:15am', '9:30am', '9:45am', '10:00am', '10:15am', '10:30am', '10:45am', '8:00pm', '8:15pm', '8:30pm', '8:45pm', '9:00pm', '9:15pm', '9:30pm', '9:45pm', '10:00pm', '10:15pm', '10:30pm', '10:45pm']

Tags:

Python