How do we compare Python Dates?


In this article, we will understand how to compare python dates. They are different methods to identify which date is greater or lesser and these methods will be explored in detail.

Using the timedelta() method and operator

In this method, we use the datetime module and operator to compare two dates. To alter date and time, the datatime module provides timedelta() method. The timedelta() method takes the number of days as an input and returns the date. This method is used to perform arithmetic manipulations.

Syntax

The syntax of the timedelta() method from the datetime module in python is given below.

datetime.timedelta(days=0, seconds=0, microseconds=0, milliseconds=0, minutes=0, hours=0, weeks=0)

Example

In the following example code, we compare two dates using the timedelta() method in python.

from datetime import datetime, timedelta date_before_2days = datetime.now() - timedelta(days=2) current_date = datetime.now() print(current_date > date_before_2days)

Output

The output for the above code is as follows.

True

Using the datetime.date() method

Here we use the datetime.date() method to compare two dates. We import datetime module to work with date as date objects. The datetime.date() method takes year,month and day as input returns the date.

Syntax

The syntax of the datetime.date() method is given below.

datetime.date(year, month, day)

Example

In the following example code, we compare two dates using the datetime.date() method in python.

import datetime date1 = datetime.date(2018, 6, 9) date2 = datetime.date(2018, 1, 23 ) print("date1 is greater than date2 : ", date1 > date2) print("date1 is less than date2 : ", date1 < date2) print("date1 is not equal to date2 : ", date1 != date2)

Output

The output for the above code is as follows.

date1 is greater than date2 :  True
date1 is less than date2 :  False
date1 is not equal to date2 :  True

Using the strptime() method from the time module

In this method, we use the strptime() method from the time module to compare two dates. The strptime() method takes the date as an input and converts it into a date object.

Syntax

The syntax of the strptime() method is as follows.

datetime.strptime(time_data, format_data)

Where,

  • ime_date − it is the time present in string format.

  • format_date − it is the data present in datetime format which is converted from time_data using this function.

Example

Here we used the strptime() method to compare two dates in python.

import time date1 = "23/01/2020" date2 = "28/02/2019" format_date1 = time.strptime(date1, "%d/%m/%Y") format_date2 = time.strptime(date2, "%d/%m/%Y") print(format_date1 > format_date2)

Output

The output produced for the above program can be seen below.

True

Using the datetime() method from the datetime module

In this method, we use the datetime() method to compare two dates. The datetime module provides the datetime() method to create a date object by taking three parameters (date, month, year).

Syntax

The syntax for the datetime() method is as follows.

datetime.datetime(year,month,day)

Example

In the following example code, we compare two dates using the datetime.datetime() method in python.

import datetime date1 = datetime.datetime(2018, 6, 9) date2 = datetime.datetime(2018, 1, 23 ) print("date1 is greater than date2 : ", date1 > date2) print("date1 is less than date2 : ", date1 < date2) print("date1 is not equal to date2 : ", date1 != date2)

Output

The output produced for the above program is as follows.

date1 is greater than date2 :  True
date1 is less than date2 :  False
date1 is not equal to date2 :  True

Updated on: 05-Sep-2022

536 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements