Creating a list of range of dates in Python
Last Updated :
23 Mar, 2023
Given a date, the task is to write a Python program to create a list of a range of dates with the next K dates starting from the current date.
Examples:
Input : test_date = datetime.datetime(1997, 1, 4), K = 5
Output : [datetime.datetime(1997, 1, 4), datetime.datetime(1997, 1, 5), datetime.datetime(1997, 1, 6), datetime.datetime(1997, 1, 7), datetime.datetime(1997, 1, 8)]
Explanation : 5 dates after 4 January are extracted in list.
Creating a list of dates using pd.date_range
In this method, we will use pandas date_range to create a list of ranges of dates in Python.
Python3
import datetime
import pandas as pd
test_date = datetime.datetime.strptime( "01-7-2022" , "%d-%m-%Y" )
K = 5
date_generated = pd.date_range(test_date, periods = K)
print (date_generated.strftime( "%d-%m-%Y" ))
|
Output:
Index(['01-07-2022', '02-07-2022', '03-07-2022', '04-07-2022', '05-07-2022'], dtype='object')
In this, we get to add consecutive deltas to day using timedelta(), and list comprehension is used to iterate through the required size and construct the required result.
Python3
import datetime
test_date = datetime.datetime( 1997 , 1 , 4 )
K = 5
res = [test_date + datetime.timedelta(days = idx) for idx in range (K)]
print ( "Next K dates list : " + str (res))
|
Output:
Next K dates list : [datetime.datetime(1997, 1, 4, 0, 0), datetime.datetime(1997, 1, 5, 0, 0),
datetime.datetime(1997, 1, 6, 0, 0), datetime.datetime(1997, 1, 7, 0, 0), datetime.datetime(1997, 1, 8, 0, 0)]
Creating a list of dates using Python Loop
In this, we perform a similar task as the above function, using a generator to perform the task of Date successions.
Python3
import datetime
start = datetime.date( 2022 , 8 , 12 )
K = 5
res = []
for day in range (k):
date = (start + datetime.timedelta(days = day)).isoformat()
res.append(date)
print ( "Next K dates list: " + str (res))
|
Output:
Next K dates list: ['2022-08-12', '2022-08-13', '2022-08-14', '2022-08-15', '2022-08-16']
Approach:
Import the required modules datetime and timedelta.
Define the start and end dates using the datetime module.
Initialize an empty list to store the generated dates.
Use a while loop that iterates through each day between the start and end dates.
Append each date to the date_list using the append method.
Increment the date by one day using the timedelta module.
Print the final date_list.
Python3
from datetime import datetime, timedelta
start_date = datetime( 2022 , 3 , 1 )
end_date = datetime( 2022 , 3 , 10 )
date_list = []
while start_date < = end_date:
date_list.append(start_date)
start_date + = timedelta(days = 1 )
print (date_list)
|
Output
[datetime.datetime(2022, 3, 1, 0, 0), datetime.datetime(2022, 3, 2, 0, 0), datetime.datetime(2022, 3, 3, 0, 0), datetime.datetime(2022, 3, 4, 0, 0), datetime.datetime(2022, 3, 5, 0, 0), datetime.datetime(2022, 3, 6, 0, 0), datetime.datetime(2022, 3, 7, 0, 0), datetime.datetime(2022, 3, 8, 0, 0), datetime.datetime(2022, 3, 9, 0, 0), datetime.datetime(2022, 3, 10, 0, 0)]
This approach has a time complexity of O(n) where n is the number of dates in the range
An auxiliary space of O(n) to store the list of dates.
Using a generator and timedelta:
Approach steps:
Define the function date_range_4 that takes two arguments – test_date and K.
Initialize a variable i to 0.
Enter a loop that runs K times.
Inside the loop, yield the current date, i.e., test_date plus i days.
Increment i by 1 after each iteration.
Call the date_range_4 function with test_date and K as arguments.
Convert the generator object returned by the function to a list using the list() function.
Print the list of dates.
Python3
import datetime
def date_range_4(test_date, K):
i = 0
while i < K:
yield test_date + datetime.timedelta(days = i)
i + = 1
test_date = datetime.datetime( 1997 , 1 , 4 )
K = 5
date_list = list (date_range_4(test_date, K))
print (date_list)
|
Output
[datetime.datetime(1997, 1, 4, 0, 0), datetime.datetime(1997, 1, 5, 0, 0), datetime.datetime(1997, 1, 6, 0, 0), datetime.datetime(1997, 1, 7, 0, 0), datetime.datetime(1997, 1, 8, 0, 0)]
Time complexity: O(K)
Space complexity: O(1) (since the generator yields one date at a time without storing all the dates in memory)
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...