Skip to content
Related Articles

Related Articles

Improve Article

Creating a list of range of dates in Python

  • Last Updated : 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.

Method #1 : Using timedelta() + list comprehension

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 comphehension
import datetime
  
# initializing date
test_date = datetime.datetime(1997, 1, 4)
               
# printing original date
print("The original date is : " + str(test_date))
  
# initializing K 
K = 5
  
# timedelta() gets successive dates with 
# appropriate difference
res = [test_date + datetime.timedelta(days=idx) for idx in range(K)]
  
# printing result
print("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)]



Method #2 : Using count() + generator function

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 function
import datetime
import itertools
  
# initializing date
test_date = datetime.datetime(1997, 1, 4)
               
# printing original date
print("The original date is : " + str(test_date))
  
# initializing K 
K = 5
  
# timedelta() gets successive dates with 
# appropriate difference
gen_fnc = (
  test_date - datetime.timedelta(days=idx) for idx in itertools.count())
  
# islice passes counter
res = itertools.islice(gen_fnc, K)
  
# printing result
print("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)]

 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




My Personal Notes arrow_drop_up
Recommended Articles
Page :