Skip to content
Related Articles

Related Articles

Python Program to check date in date range

View Discussion
Improve Article
Save Article
  • Last Updated : 06 Jun, 2021
View Discussion
Improve Article
Save Article

Given a date list and date range, the task is to write a Python program to check whether any date exists in the list in a given range.

Example:

Input : test_list = [datetime(2019, 12, 30), datetime(2018, 4, 4), datetime(2016, 12, 21), datetime(2021, 2, 2), datetime(2020, 2, 3), datetime(2017, 1, 1)], date_strt, date_end = datetime(2019, 3, 14), datetime(2020, 1, 4)

Output : True

Explanation : 30 Dec’2019 lies in range of 14 March 2019 to 4 January 2020, hence True.

Input : test_list = [datetime(2018, 4, 4), datetime(2016, 12, 21), datetime(2021, 2, 2), datetime(2020, 2, 3), datetime(2017, 1, 1)], date_strt, date_end = datetime(2019, 3, 14), datetime(2020, 1, 4)

Output : False

Explanation : No date lies in range.

Method 1: Using loop

In this, for each element, we check using conditionals if any date falls in the range, if found, true is returned.

Python3




# Python3 code to demonstrate working of
# Test for date in date range
# Using loop
from datetime import datetime
  
# initializing list
test_list = [datetime(2019, 12, 30), datetime(2018, 4, 4), 
             datetime(2016, 12, 21), datetime(2021, 2, 2), 
             datetime(2020, 2, 3), datetime(2017, 1, 1)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing date ranges
date_strt, date_end = datetime(2019, 3, 14), datetime(2020, 1, 4)
  
res = False
for ele in test_list:
  
    # checking for date in range
    if ele >= date_strt and ele <= date_end:
        res = True
  
# printing result
print("Does list contain any date in range : " + str(res))

Output:

The original list is : [datetime.datetime(2019, 12, 30, 0, 0), datetime.datetime(2018, 4, 4, 0, 0), datetime.datetime(2016, 12, 21, 0, 0), datetime.datetime(2021, 2, 2, 0, 0), datetime.datetime(2020, 2, 3, 0, 0), datetime.datetime(2017, 1, 1, 0, 0)]

Does list contain any date in range : True

Method 2: Using any()

Similar to the above method, the only difference being any() is used to check for the presence of any date in range.

Python3




# Python3 code to demonstrate working of
# Test for date in date range
# Using any()
from datetime import datetime
  
# initializing list
test_list = [datetime(2019, 12, 30), datetime(2018, 4, 4),
             datetime(2016, 12, 21), datetime(2021, 2, 2), 
             datetime(2020, 2, 3), datetime(2017, 1, 1)]
  
# printing original list
print("The original list is : " + str(test_list))
  
# initializing date ranges
date_strt, date_end = datetime(2019, 3, 14), datetime(2020, 1, 4)
  
# any() for checking presence of any date in range
res = any(ele >= date_strt and ele <= date_end for ele in test_list)
  
# printing result
print("Does list contain any date in range : " + str(res))

Output:

The original list is : [datetime.datetime(2019, 12, 30, 0, 0), datetime.datetime(2018, 4, 4, 0, 0), datetime.datetime(2016, 12, 21, 0, 0), datetime.datetime(2021, 2, 2, 0, 0), datetime.datetime(2020, 2, 3, 0, 0), datetime.datetime(2017, 1, 1, 0, 0)]

Does list contain any date in range : True


My Personal Notes arrow_drop_up
Recommended Articles
Page :

Start Your Coding Journey Now!