Open In App

Python Program to check date in date range

Last Updated : 16 May, 2023
Improve
Improve
Like Article
Like
Save
Share
Report

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

Time Complexity: O(n), Since we are looping through the list of elements, the time complexity of this algorithm is O(n) where n is the number of elements in the list.
Auxiliary Space: O(1), Since we are not using any extra space, the space complexity of this algorithm is O(1).

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

Time complexity: O(n), where n is the length of the test_list.
Auxiliary space: O(1),

Method 3: Using filter() and lambda function

Filter function can be used to filter out the elements from a list based on a given condition. We can use this function with a lambda function that takes each element from the test_list and returns True if it falls in the date range, False otherwise. If the resulting filtered list is not empty, then we know that at least one element in the original list falls within the date range.

Python3




# Python3 code to demonstrate working of
# Test for date in date range
# Using filter() and lambda function
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)
 
# filtering the list to get elements in date range
filtered_list = list(filter(lambda x: x >= date_strt and x <= date_end, test_list))
 
# printing result
if filtered_list:
    print("Does list contain any date in range : True")
else:
    print("Does list contain any date in range : False")


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

Time complexity: The time complexity of this implementation is O(n), where n is the length of the test_list. 
Auxiliary Space: The space complexity of this implementation is O(k), where k is the number of elements that fall within the date range. 

Method 4 : using list comprehension. 

Step-by-step approach:

  • Use list comprehension to check if there exists any date in the list that falls within the given date range: [date for date in test_list if date_strt <= date <= date_end]
  • Check the length of the resulting list. If it is greater than 0, then there exists at least one date in the list that falls within the given date range.
  • Print the result: if len([date for date in test_list if date_strt <= date <= date_end]) > 0: print(“Does list contain any date in range: True”) else: print(“Does list contain any date in range: False”)

Python3




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)]
 
# initializing date ranges
date_strt, date_end = datetime(2019, 3, 14), datetime(2020, 1, 4)
 
# checking if any date falls within the given date range using list comprehension
if len([date for date in test_list if date_strt <= date <= date_end]) > 0:
    print("Does list contain any date in range: True")
else:
    print("Does list contain any date in range: False")


Output

Does list contain any date in range: True

Time complexity: O(n), where n is the length of the list of dates.
Auxiliary space: O(1), as we are only using a constant amount of additional space to store the start and end dates.

Method 5 :  Using the set function

we first initializes the test_list and the date ranges date_strt and date_end. It then creates a date_range_set containing all dates in test_list that fall within the given date range using a list comprehension. Finally, it checks if the intersection of test_list and date_range_set is not empty and prints the corresponding message.

Step-by-step approach:

  • Import the datetime module.
  • Create a list of datetime objects named test_list.
  • Create two datetime objects date_strt and date_end that represent the start and end dates of the range you want to check.
  • Create a new set called date_range_set that contains all the datetime objects from test_list that fall within the range specified by date_strt and date_end.
  • Use the intersection() method to check if there are any datetime objects in test_list that are also in date_range_set.
  • Check the length of the resulting set to determine if there are any datetime objects in both sets.
  • Print the appropriate message depending on whether any datetime objects are found in the intersection of test_list and date_range_set.

Python3




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)]
 
# initializing date ranges
date_strt, date_end = datetime(2019, 3, 14), datetime(2020, 1, 4)
 
# create a set of dates within the given date range
date_range_set = set([date for date in test_list if date_strt <= date <= date_end])
 
# check if the intersection of the test_list and the date_range_set is not empty
if len(set(test_list).intersection(date_range_set)) > 0:
    print("Does list contain any date in range: True")
else:
    print("Does list contain any date in range: False")


Output

Does list contain any date in range: True

Time complexity: O(n), where n is the length of the test_list.
Auxiliary space: O(m), where m is the length of the date_range_set.

Method 6: Using numpy:

  • Import the NumPy library
  • Create a NumPy array from the original list of datetime objects
  • Use NumPy’s logical_and function to create a boolean array indicating which elements in the array fall within the given date range
  • Use NumPy’s any function to check if any element in the boolean array is True, indicating that at least one datetime object falls within the given date range
  • If any element in the boolean array is True, print “Does list contain any date in range: True”, otherwise print “Does list contain any date in range: False”

Python3




import numpy as np
 
# initializing list
test_list = [np.datetime64('2019-12-30'), np.datetime64('2018-04-04'),
             np.datetime64('2016-12-21'), np.datetime64('2021-02-02'),
             np.datetime64('2020-02-03'), np.datetime64('2017-01-01')]
# printing original list
print("The original list is : " + str(test_list))
# initializing date ranges
date_strt, date_end = np.datetime64('2019-03-14'), np.datetime64('2020-01-04')
 
# checking if any date falls within the given date range using numpy methods
if np.any(np.logical_and(test_list >= date_strt, test_list <= date_end)):
    print("Does list contain any date in range: True")
else:
    print("Does list contain any date in range: False")
 
#This code is contributed by Rayudu.


Output:
The original list is : [numpy.datetime64('2019-12-30'), numpy.datetime64('2018-04-04'), numpy.datetime64('2016-12-21'), numpy.datetime64('2021-02-02'), numpy.datetime64('2020-02-03'), numpy.datetime64('2017-01-01')]
Does list contain any date in range: True

Time complexity:

Creating the NumPy array takes O(n) time, where n is the length of the original list
Using NumPy’s logical_and and any functions takes O(1) time, as these operations are implemented efficiently in the NumPy library
Therefore, the overall time complexity of the algorithm is O(n)
Auxiliary Space:

Creating the NumPy array takes O(n) space, as the array must store all of the datetime objects from the original list
The boolean array created by the logical_and function takes O(n) space as well, as it has the same length as the NumPy array
Therefore, the overall space complexity of the algorithm is O(n)

Method 7: Using the bisect module

  • Import the bisect module
  • Sort the test_list using the sort() method.
  • Use bisect_left() and bisect_right() methods to find the index of the first and last elements within the given date range, respectively.
  • If the index of the last element is greater than or equal to the index of the first element, it means that the list contains at least one date within the given range.
  • Print the result.

Python3




from datetime import datetime
 
# import the bisect module
import bisect
 
# 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)]
 
# initializing date ranges
date_strt, date_end = datetime(2019, 3, 14), datetime(2020, 1, 4)
 
# sort the test_list
test_list.sort()
 
# find the index of the first and last elements within the date range
start_index = bisect.bisect_left(test_list, date_strt)
end_index = bisect.bisect_right(test_list, date_end)
 
# check if there is any overlap between the two ranges
if end_index - start_index > 0:
    print("Does list contain any date in range: True")
else:
    print("Does list contain any date in range: False")


Output

Does list contain any date in range: True

Time complexity: O(log n) for finding the indices using bisect_left() and bisect_right() methods. Sorting the list takes O(n log n) time. Thus, the overall time complexity is O(n log n).
Auxiliary space: O(1), as we are not using any extra space except for the variables.



Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads