Python Program to check date in date range
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 loopfrom datetime import datetime # initializing listtest_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 listprint("The original list is : " + str(test_list)) # initializing date rangesdate_strt, date_end = datetime(2019, 3, 14), datetime(2020, 1, 4) res = Falsefor ele in test_list: # checking for date in range if ele >= date_strt and ele <= date_end: res = True # printing resultprint("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 listtest_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 listprint("The original list is : " + str(test_list)) # initializing date rangesdate_strt, date_end = datetime(2019, 3, 14), datetime(2020, 1, 4) # any() for checking presence of any date in rangeres = any(ele >= date_strt and ele <= date_end for ele in test_list) # printing resultprint("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
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


