• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Programming Expert

Solving All of Your Programming Headaches

  • HTML
  • JavaScript
  • jQuery
  • PHP
  • Python
  • SAS
  • Ruby
  • About
You are here: Home / Python / Comparing Datetimes in Python

Comparing Datetimes in Python

March 8, 2022 Leave a Comment

In Python, we can easily compare two datetimes to see which datetime is later than another with >, < and == operators just like when comparing numbers.

import datetime

datetime1 = datetime.datetime(2022,3,5,0,0,0)
datetime2 = datetime.datetime(2022,3,8,12,30,0)

print(datetime1 < datetime2)
print(datetime1 > datetime2)
print(datetime1 == datetime2)

#Output:
True
False
False

You can also use these same operators to compare two dates in Python.

import datetime

datetime1 = datetime.date(2022,3,5)
datetime2 = datetime.date(2022,3,8)

print(datetime1 < datetime2)
print(datetime1 > datetime2)
print(datetime1 == datetime2)

#Output:
True
False
False

When working in Python, many times we need to create variables which represent dates and times. Being able to easily determine which dates or datetime variables are later or before other variables is very valuable.

We can easily compare datetimes in Python using the standard comparison operators >, < and ==.

Below is a simple example of comparing two datetimes in Python.

import datetime

datetime1 = datetime.datetime(2022,3,5,0,0,0)
datetime2 = datetime.datetime(2022,3,8,12,30,0)

print(datetime1 < datetime2)
print(datetime1 > datetime2)
print(datetime1 == datetime2)

#Output:
True
False
False

You can also use these same operators to compare two dates in Python.

import datetime

datetime1 = datetime.date(2022,3,5)
datetime2 = datetime.date(2022,3,8)

print(datetime1 < datetime2)
print(datetime1 > datetime2)
print(datetime1 == datetime2)

#Output:
True
False
False

How to Check if a datetime is Later Than Another datetime in Python

To check if a datetime is later than another datetime, use the > operator.

Below is a simple example in Python of how to compare datetimes to see which datetime is later than the other.

import datetime

datetime1 = datetime.datetime(2022,3,5,0,0,0)
datetime2 = datetime.datetime(2022,3,8,12,30,0)

print(datetime1 > datetime2)

#Output:
False

How to Check if a datetime is Earlier Than Another datetime in Python

To check if a datetime is earlier than another datetime, use the < operator.

Below is a simple example in Python of how to compare datetimes to see which datetime is earlier than the other.

import datetime

datetime1 = datetime.datetime(2022,3,5,0,0,0)
datetime2 = datetime.datetime(2022,3,8,12,30,0)

print(datetime1 < datetime2)

#Output:
True

How to Check if a datetime is Equal to Another datetime in Python

To check if a datetime is equal to another datetime, use the == operator.

Below is a simple example in Python of how to compare datetimes to see which datetime is equal to another.

import datetime

datetime1 = datetime.datetime(2022,3,5,0,0,0)
datetime2 = datetime.datetime(2022,3,8,12,30,0)

print(datetime1 == datetime2)

#Output:
False

Comparing Dates of Two datetime Objects in Python

If you just want to compare the dates of datetime objects in Python, we can easily do that by calling the date() function.

The date() function removes the time from the datetime. Then, you can use the comparison operators.

Below is an example of comparing just the dates of two datetimes in Python.

import datetime

datetime1 = datetime.datetime(2022,3,5,0,0,0)
datetime2 = datetime.datetime(2022,3,8,12,30,0)

print(datetime1.date() < datetime2.date())
print(datetime1.date() > datetime2.date())
print(datetime1.date() == datetime2.date())

#Output:
True
False
False

Comparing Times of Two datetime Objects in Python

If you just want to compare the times of datetime objects in Python, we can easily do that by calling the time() function.

The time() function removes the date from the datetime. Then, you can use the comparison operators.

Below is an example of comparing just the times of two datetimes in Python.

import datetime

datetime1 = datetime.datetime(2022,3,5,0,0,0)
datetime2 = datetime.datetime(2022,3,8,12,30,0)

print(datetime1.time() < datetime2.time())
print(datetime1.time() > datetime2.time())
print(datetime1.time() == datetime2.time())

#Output:
True
False
False

Hopefully this article has been useful for you to learn how to compare datetimes in Python.

Other Articles You'll Also Like:

  • 1.  Using Python to Read File Character by Character
  • 2.  Golden Ratio Constant phi in Python
  • 3.  Check if String is Date in Python
  • 4.  How to Group By Columns and Find Mean in pandas DataFrame
  • 5.  Using pandas sample() to Generate a Random Sample of a DataFrame
  • 6.  Remove Trailing Zeros from String with rstrip() in Python
  • 7.  Sort Series in pandas with sort_values() Function
  • 8.  How to Remove All Spaces from String in Python
  • 9.  Python Decrement Counter with -= Decrement Operator
  • 10.  Python isdigit() Function – Check if Characters in String are Digits

About The Programming Expert

The Programming Expert is a compilation of a programmer’s findings in the world of software development, website creation, and automation of processes.

Programming allows us to create amazing applications which make our work more efficient, repeatable and accurate.

At the end of the day, we want to be able to just push a button and let the code do it’s magic.

You can read more about us on our about page.

Reader Interactions

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Primary Sidebar

About The Programming Expert

the programming expert main image

Welcome to The Programming Expert. We are a group of US-based programming professionals who have helped companies build, maintain, and improve everything from simple websites to large-scale projects.

We built The Programming Expert to help you solve your programming problems with useful coding methods and functions in various programming languages.

Search

Learn Coding from Experts on Udemy

Looking to boost your skills and learn how to become a programming expert?

Check out the links below to view Udemy courses for learning to program in the following languages:

Copyright © 2023 · The Programming Expert · About · Privacy Policy