Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Python Program to check date in date range
In this article we will learn how to check if a given input date falls within a specified date range. Python provides several approaches to accomplish this task, each with its own advantages.
Using the datetime() Function
The datetime module offers classes to manipulate dates and times. The datetime() function accepts year, month, and day as input arguments and returns a datetime object that can be easily compared.
Example
In this example we check if the date 25052023 falls within the range of 01011991 to 31122023 ?
from datetime import datetime
def is_date_in_range(date, start_date, end_date):
return start_date <= date <= end_date
# Define the dates
date = datetime(2023, 5, 25)
start_date = datetime(1991, 1, 1)
end_date = datetime(2023, 12, 31)
# Check if date is in range
if is_date_in_range(date, start_date, end_date):
print("Date is within the range.")
else:
print("Date is outside the range.")
Date is within the range.
Using the date() Function
The date() function from the datetime module creates date objects without time information, making it ideal for dateonly comparisons.
Example
Here we create a function to check if a date falls within a specific year range ?
from datetime import date
def is_date_in_range(input_date, start_date, end_date):
return start_date <= input_date <= end_date
# Define the dates using date() function
input_date = date(2023, 5, 10)
start_date = date(2023, 1, 1)
end_date = date(2023, 12, 31)
# Check if date is in range
if is_date_in_range(input_date, start_date, end_date):
print(f"{input_date} is within the range.")
else:
print(f"{input_date} is outside the range.")
2023-05-10 is within the range.
Using String Comparison
For dates in ISO format (YYYYMMDD), string comparison works because the lexicographical order matches chronological order. This method is simple but less robust than datetime objects.
Example
In this example we compare dates in string format using the ISO date format ?
def is_date_in_range(date, start_date, end_date):
return start_date <= date <= end_date
# Define dates as strings in ISO format (YYYY-MM-DD)
date = "2023-05-25"
start_date = "1995-01-01"
end_date = "2023-12-09"
# Check if date is in range
if is_date_in_range(date, start_date, end_date):
print(f"{date} is within the range.")
else:
print(f"{date} is outside the range.")
2023-05-25 is within the range.
Comparison of Methods
| Method | Advantages | Best For |
|---|---|---|
datetime() |
Includes time, robust validation | Complex datetime operations |
date() |
Dateonly, memory efficient | Simple date comparisons |
| String comparison | Simple, no imports needed | Quick checks with ISO format |
Conclusion
Use datetime() objects for robust date range checking with builtin validation. For simple dateonly comparisons, the date() function is most efficient. String comparison works only with ISO format dates.
