Comparing dates in Python Last Updated : 11 Jul, 2025 Summarize Comments Improve Suggest changes Share Like Article Like Report In Python, comparing dates is straightforward with the help of the datetime module. You can use basic comparison operators like <, >, ==, and != to compare two date or datetime objects directly. Below are a few common and practical ways to compare and sort dates in Python.Basic Date ComparisonYou can directly compare two datetime objects using standard comparison operators. Python from datetime import datetime d1 = datetime(2018, 5, 3) d2 = datetime(2018, 6, 1) print("d1 > d2:", d1 > d2) print("d1 < d2:", d1 < d2) print("d1 != d2:", d1 != d2) Outputd1 > d2: False d1 < d2: True d1 != d2: True Explanation:datetime() creates a date-time object.operators like >, <, and != work directly on datetime objects.Sorting a List of DatesDates can be stored in a list and sorted using the sort() method. Python from datetime import date, timedelta dates = [ date.today(), date(2015, 6, 29), date(2011, 4, 7), date(2011, 4, 7) + timedelta(days=25) ] dates.sort() for d in dates: print(d) Output2011-04-07 2011-05-02 2015-06-29 2025-04-22 Explanation:date.today() returns the current date.timedelta(days=25) adds days to a date.sort() arranges the list of dates in ascending order.Using timedelta for ComparisonWe can also use subtraction between date objects and compare the result using timedelta. Python from datetime import date, timedelta d1 = date(2022, 4, 1) d2 = date(2023, 4, 1) print("d1 > d2:", d1 - d2 > timedelta(0)) print("d1 < d2:", d1 - d2 < timedelta(0)) print("d1 != d2:", d1 != d2) Outputd1 > d2: False d1 < d2: True d1 != d2: True Explanation:Subtracting two dates returns a timedelta object.Comparing with timedelta(0) shows which date is earlier/later.!= checks if the dates are different.Also read: sort(), timedelta, datetime object. Comment More infoAdvertise with us Next Article Compare two Files line by line in Python H harsshmalik Follow Improve Article Tags : Python date-time-program python-modules Practice Tags : python Similar Reads Comparison Operators in Python Python operators can be used with various data types, including numbers, strings, boolean and more. In Python, comparison operators are used to compare the values of two operands (elements being compared). When comparing strings, the comparison is based on the alphabetical order of their characters 4 min read String Comparison in Python Python supports several operators for string comparison, including ==, !=, <, <=, >, and >=. These operators allow for both equality and lexicographical (alphabetical order) comparisons, which is useful when sorting or arranging strings.Letâs start with a simple example to illustrate the 3 min read Python Object Comparison : "is" vs "==" In Python, both is and == are used for comparison, but they serve different purposes:== (Equality Operator) â Compares values of two objects.is (Identity Operator) â Compares memory location of two objects.Pythona = [1,2,3] b = [1,2,3] print(a == b) print(a is b) OutputTrue False Explanation: a and 2 min read Python | Decimal compare() method Decimal#compare() : compare() is a Decimal class method which compares the two Decimal values. Syntax: Decimal.compare() Parameter: Decimal values Return: 1 - if a > b -1 - if a < b 0 - if a = b Code #1 : Example for compare() method Python3 # Python Program explaining # compare() method # loa 2 min read Compare two Files line by line in Python In Python, there are many methods available to this comparison. In this Article, We'll find out how to Compare two different files line by line. Python supports many modules to do so and here we will discuss approaches using its various modules. This article uses two sample files for implementation. 3 min read numpy.less() in Python The numpy.less() : checks whether x1 is lesser than x2 or not. Syntax : numpy.less(x1, x2[, out]) Parameters : x1, x2 : [array_like]Input arrays. If x1.shape != x2.shape, they must be broadcastable to a common shape out : [ndarray, boolean]Array of bools, or a single bool if x1 and x2 are scalars. R 2 min read Like