Creating a list of range of dates in Python - GeeksforGeeks

09 May, 2021
Given a date, and the task is to write a Python program to create a list of 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, 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)]
Explanation : 5 dates after 4 January are extracted in list.
Input : test_date = datetime.datetime(1997, 1, 4), K = 4
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)]
Explanation : 4 dates after 4 January are extracted in list.
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
# Python3 code to demonstrate working of# Get Construct Next K dates List# Using timedelta() + list comphehensionimport datetime  # initializing datetest_date = datetime.datetime(1997, 1, 4)               # printing original dateprint("The original date is : " + str(test_date))  # initializing K K = 5  # timedelta() gets successive dates with # appropriate differenceres = [test_date + datetime.timedelta(days=idx) for idx in range(K)]  # printing resultprint("Next K dates list : " + str(res))
Output:
The original date is : 1997-01-04 00:00:00
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)]
In this, we perform a similar task as the above function, using a generator to perform the task of date successions.
Python3
# Python3 code to demonstrate working of# Get Construct Next K dates List# Using count() + generator functionimport datetimeimport itertools  # initializing datetest_date = datetime.datetime(1997, 1, 4)               # printing original dateprint("The original date is : " + str(test_date))  # initializing K K = 5  # timedelta() gets successive dates with # appropriate differencegen_fnc = (  test_date - datetime.timedelta(days=idx) for idx in itertools.count())  # islice passes counterres = itertools.islice(gen_fnc, K)  # printing resultprint("Next K dates list : " + str(list(res)))
Output:
The original date is : 1997-01-04 00:00:00
Next K dates list : [datetime.datetime(1997, 1, 4, 0, 0), datetime.datetime(1997, 1, 3, 0, 0), 
datetime.datetime(1997, 1, 2, 0, 0), datetime.datetime(1997, 1, 1, 0, 0), datetime.datetime(1996, 12, 31, 0, 0)]
Python datetime-program
Python
Python Programs
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
Comments
Old Comments
How to Install PIP on Windows ?
Selecting rows in pandas DataFrame based on conditions
How to drop one or multiple columns in Pandas Dataframe
How To Convert Python Dictionary To JSON?
Check if element exists in list in Python
Defaultdict in Python
Python | Get dictionary keys as a list
Python | Split string into list of characters
Python | Convert a list to dictionary
Python program to check whether a number is Prime or not