Skip to content
Advertisement

Split Time range into multiple time periods based on interval in Python

I have a time-range and an interval, I need to split the time range into multiple time periods based on interval value.

For example, time range is 9:30 to 11:30 and the interval is 30, the output time periods should be in a list as datetime objects

Output:

[
2020-08-24 9:30 - 2020-08-24 10:00,
2020-08-24 10:00 - 2020-08-24 10:30 
2020-08-24 10:30 - 2020-08-24 11:00, 
2020-08-24 11:00 - 2020-08-24 11:30
]

Advertisement

Answer

You can do arithmetic on datetime objects by adding timedelta objects.

You probably need to decide exactly what behaviour is required if the interval per period is not an exact divisor of the total, but this example would give a final short period in that case.

import datetime

tstart = datetime.datetime(2020,8,24,9,30)
tend = datetime.datetime(2020,8,24,11,30)
interval = datetime.timedelta(minutes=30)

periods = []

period_start = tstart
while period_start < tend:
    period_end = min(period_start + interval, tend)
    periods.append((period_start, period_end))
    period_start = period_end

print(periods)

This gives (with newlines inserted for readability):

[(datetime.datetime(2020, 8, 24, 9, 30), datetime.datetime(2020, 8, 24, 10, 0)),
 (datetime.datetime(2020, 8, 24, 10, 0), datetime.datetime(2020, 8, 24, 10, 30)),
 (datetime.datetime(2020, 8, 24, 10, 30), datetime.datetime(2020, 8, 24, 11, 0)),
 (datetime.datetime(2020, 8, 24, 11, 0), datetime.datetime(2020, 8, 24, 11, 30))]

For the string output format that you want, you could do something like this:

def format_time(dt):
    return dt.strftime("%Y-%m-%d %H:%M")

print(['{} - {}'.format(format_time(start), format_time(end))
       for start, end in periods])

to give:

['2020-08-24 09:30 - 2020-08-24 10:00',
 '2020-08-24 10:00 - 2020-08-24 10:30',
 '2020-08-24 10:30 - 2020-08-24 11:00',
 '2020-08-24 11:00 - 2020-08-24 11:30']
User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement