Python – Divide date range to N equal duration
Given two dates, the task is to divide it into equal time duration and get the exact time of each division point.
Input : test_date1 = datetime.datetime(1997, 1, 4), test_date2 = datetime.datetime(1997, 1, 30) N = 7
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course. And to begin with your Machine Learning Journey, join the Machine Learning - Basic Level Course
Output : [datetime.datetime(1997, 1, 4, 0, 0), datetime.datetime(1997, 1, 7, 17, 8, 34, 285714), datetime.datetime(1997, 1, 11, 10, 17, 8, 571428), datetime.datetime(1997, 1, 15, 3, 25, 42, 857142), datetime.datetime(1997, 1, 18, 20, 34, 17, 142856), datetime.datetime(1997, 1, 22, 13, 42, 51, 428570), datetime.datetime(1997, 1, 26, 6, 51, 25, 714284)]
Explanation : List of dates of size 7 are returned with each date having equal delta between them.
Input : test_date1 = datetime.datetime(1997, 1, 4), test_date2 = datetime.datetime(1997, 1, 30) N = 4
Output : [datetime.datetime(1997, 1, 4, 0, 0), datetime.datetime(1997, 1, 10, 12, 0), datetime.datetime(1997, 1, 17, 0, 0), datetime.datetime(1997, 1, 23, 12, 0)]
Explanation : List of dates of size 4 are returned with each date having equal delta between them.
Method #1 : Using loop
In this, we compute each segment duration using division of whole duration by N. Post that, each date is built using segment duration multiplication in loop.
Python3
# Python3 code to demonstrate working of# Convert date range to N equal durations# Using loopimport datetime # initializing datestest_date1 = datetime.datetime(1997, 1, 4)test_date2 = datetime.datetime(1997, 1, 30) # printing original datesprint("The original date 1 is : " + str(test_date1))print("The original date 2 is : " + str(test_date2)) # initializing NN = 7 temp = [] # getting diff.diff = ( test_date2 - test_date1) // Nfor idx in range(0, N): # computing new dates temp.append((test_date1 + idx * diff)) # using strftime to convert to userfriendly # formatres = []for sub in temp: res.append(sub.strftime("%Y/%m/%d %H:%M:%S")) # printing resultprint("N equal duration dates : " + str(res)) |
Output:
The original date 1 is : 1997-01-04 00:00:00
The original date 2 is : 1997-01-30 00:00:00
N equal duration dates : [‘1997/01/04 00:00:00’, ‘1997/01/07 17:08:34’, ‘1997/01/11 10:17:08’, ‘1997/01/15 03:25:42’, ‘1997/01/18 20:34:17’, ‘1997/01/22 13:42:51’, ‘1997/01/26 06:51:25’]
Method #2 : Using generator function
In this, we perform task of yielding intermediate result using generator function rather than loop approach. Only difference is that intermediate result is returned.
Python3
# Python3 code to demonstrate working of# Convert date range to N equal durations# Using generator functionimport datetime def group_util(test_date1, test_date2, N): diff = (test_date2 - test_date1 ) / N for idx in range(N): # using generator function to solve problem # returns intermediate result yield (test_date1 + diff * idx) yield test_date2 # initializing datestest_date1 = datetime.datetime(1997, 1, 4)test_date2 = datetime.datetime(1997, 1, 30) # printing original datesprint("The original date 1 is : " + str(test_date1))print("The original date 2 is : " + str(test_date2)) # initializing NN = 7 # calling generator expressiontemp = list(group_util(test_date1, test_date2, N)) # using strftime to convert to userfriendly formatres = []for sub in temp: res.append(sub.strftime("%Y/%m/%d %H:%M:%S")) # printing resultprint("N equal duration dates : " + str(res)) |
Output:
The original date 1 is : 1997-01-04 00:00:00
The original date 2 is : 1997-01-30 00:00:00
N equal duration dates : [‘1997/01/04 00:00:00’, ‘1997/01/07 17:08:34’, ‘1997/01/11 10:17:08’, ‘1997/01/15 03:25:42’, ‘1997/01/18 20:34:17’, ‘1997/01/22 13:42:51’, ‘1997/01/26 06:51:25’, ‘1997/01/30 00:00:00’]


