There are many applications that rely on date comparisons, including data analysis, web development, etc. For working with dates and times, Python offers a variety of built-in modules and functions. Some of the modules are the time module, datetime module, etc.
This post aims to compare two dates using the following content:
- Using Comparison Operator
- Using datetime.datetime() Function
- Using time.strptime() Function
- Using datetime.date() Function
- Compare Present Date With Previous Date
Using Comparison Operator
The comparison operators (like <, >, <=, >=, !=, etc.) are used to compare dates in Python. This method is simple and easy to use. This method is used to compare two dates in Python. To help you understand, here are some examples:
Example 1: Using datetime.datetime() Function
The following example is used to compare two dates created with the help of the “datetime.datetime()” function:
import datetime date_1 = datetime.datetime(2022, 3, 13) date_2 = datetime.datetime(2022, 6, 23) print(date_1 > date_2) print(date_1 < date_2)
- The “datetime.datetime()” function of the datetime module is used to represent the date and stored in a variable named “date_1” and “date_2”.
- The comparison operator “<” and “>”‘ is used to compare the two dates and return a boolean value if the condition is fulfilled.
The two given dates have been compared.
Example 2: Using time.strptime() Function
The “time.strptime()” function is used to format and return a string representation of date and time. The below code compares two dates represented by the “time.strptime()” function:
import time date1 = time.strptime("13/11/2022", "%d/%m/%Y") date2 = time.strptime("2/2/2022", "%d/%m/%Y") print(date1 > date2) print(date1 < date2)
- The “time.strptime()” function accepts two arguments: the first is the date string to be parsed, and the second is the format string that specifies the order of the date elements in the string.
- In this case, the format string “%d/%m/%Y” is used.
- The comparison operator compares the two dates and retrieves a “True” or “False” boolean value.
The two given dates have been compared.
Example 3: Using datetime.date() Function
Python’s datetime module contains a “datetime.date()” function that represents dates (years, months, and days) using the Gregorian calendar. Here is an example of how we can use this to compare two dates:
import datetime date_1 = datetime.date(2022, 12, 26) date_2 = datetime.date(2014, 11, 16) print(date_1 < date_2) print(date_1 > date_2)
- The datetime.date() function creates two date objects, date_1 and date_2.
- The comparison operator displays the True if “date_1” is less than “date_2” and False otherwise.
Two dates have been compared.
Example 4: Compare Present Date With Previous Date
The below example is used to compare the present date with the previous date:
from datetime import datetime, timedelta date1 = datetime.now() - timedelta(days=1) date2 = datetime.now() print(date2 > date1) print(date2 < date1)
- The datetime module and the timedelta class from the datetime module are imported.
- The two datetime objects “date1” and “date2” are created.
- The “date1” contains the current date and time minus one day, which means yesterday, and the “date2” object contains the current date and time.
- A comparison operator is used to determine whether “date2” is greater than “date1” or whether “date2” is less than “date1”.
The dates of two events have been compared.
To compare two dates, the comparison operators are used along with the “datetime.datetime()” function, “time.strptime()”, and “datetime.date()” function. These functions are used to input or represent the two specific date objects and are compared using the comparison operators. The current date with the previous specific date is also compared using the datetime module. An in-depth overview of Python comparing two dates is presented in this guide, along with numerous examples.