Check Date in Date Range using Python



In this article we will learn how to check if the given input date falls in the specified date range or not. There are ways to do so; some of them are explained in detail.

Using the datetime module

Datetime module offers classes to manipulate the dates and times. This module provides various functions and methods such as date, time, datetime, timedelta, minyear, maxyear, UTC etc.

Here in this article we will use the datetime() function of the datetime module. The datetime() accepts the date, month and year as the input arguments and returns the date as output.

Example

In this example we are going to check if the date 25052023 is within the date range of 01011991 and 31122023 by using the datetime() function of the datetime module.

Open Compiler
from datetime import datetime def is_date_in_range(date, start_date, end_date): return start_date <= date <= end_date date = datetime(2023, 5, 25) start_date = datetime(1991, 1, 1) end_date = datetime(2023, 12, 31) if is_date_in_range(date, start_date, end_date): print("Date is within the range.") else: print("Date is outside the range.")

Output

Date is within the range.

Using the date() function

In the datetime module we have the other function date(), which accepts the date as the input arguments as date, month and year, and frames the date and returns it as output.

Example

In this example we are creating a function with the name is_date_in_range() by passing the input date, start date and end date as the input arguments along the condition for checking the date in the given range or not. The dates are given using the date() function of the datetime module.

Open Compiler
from datetime import date def is_date_in_range(input_date, start_date, end_date): return start_date <= input_date <= end_date input_date = date(2023, 5, 10) start_date = date(2023, 1, 1) end_date = date(2023, 12, 31) if is_date_in_range(input_date, start_date, end_date): print(input_date,"is within the range.") else: print(input_date,"is outside the range.")

Output

2023-05-10 is within the range.

Using string comparison

A string is the data structure that holds the data within the single quotes or double quotes. The string is defined using str() function by passing any datatype elements as the input and returns string as the output.

Here to check if the given date is within the given date range we will compare the dates which are defined in the string format.

Example

In this example we are going to define the dates in the string format and then compare the input string, with the dates within the defined date range.

Let's say the given input date as 25052023, the date range is defined as 01-01-1995 and 09122020. The below is the code.

Open Compiler
def is_date_in_range(date, start_date, end_date): return start_date <= date <= end_date date = "2023-05-25" start_date = "1995-01-01" end_date = "2023-12-09" if is_date_in_range(date, start_date, end_date): print(date,"is within the range.") else: print(date,"is outside the range.")

Output

2023-05-25 is within the range.
Updated on: 2023-10-19T14:43:57+05:30

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements