Calculate Time Difference Between Two datetime Objects in Python (Example)

 

In this post, I’ll show you how to calculate the time difference between two datetime objects in the Python programming language.

The following sections are explained in this article:

Let’s do this!

 

Import datetime Module & Create Example Dates

First, we have to import the datetime module:

import datetime

Now, we can create two example datetime objects as shown below:

date1 = datetime.datetime(2025, 10, 15, 8, 22)
print(date1)
# 2025-10-15 08:22:00
date2 = datetime.datetime(2023, 9, 27, 10, 19)
print(date2)
# 2023-09-27 10:19:00

As you can see, we have created two datetime objects called date1 and date2.

Let’s compute the time difference between these two objects!

 

Example: Calculate Time Difference Between Two datetime Objects

This example shows how to calculate the time difference between two datetime objects in Python.

For this, we can use the – operator to subtract our two dates from each other as shown below:

date_diff = date1 - date2
print(date_diff)
# 748 days, 22:03:00

The previous Python code has created a new data object called date_diff, which contains the time difference between our two example dates.

The time difference is 748 days, 22 hours, and 3 minutes.

 

Video, Further Resources & Summary

If you need more explanations on how to quantify the time difference between two datetime objects, you may have a look at the following video of the YouTube channel of Kyle Monson.

In his video, Kyle shows how to use the dateutil module, which provides powerful extensions to the standard datetime module.

 

Please accept YouTube cookies to play this video. By accepting you will be accessing content from YouTube, a service provided by an external third party.

YouTube Content Consent Button Thumbnail

YouTube privacy policy

If you accept this notice, your choice will be saved and the page will refresh.

 

Furthermore, you may have a look at some of the other tutorials on this website:

Summary: This article explained to you how to determine and get the time difference between two datetime objects in the Python programming language. For additional questions, you may leave a comment below.

 

Gottumukkala Sravan Kumar Statistician & Programmer

Note: This article was created in collaboration with Gottumukkala Sravan Kumar. Gottumukkala is a data analyst and programmer who helps to create tutorials on topics such as the datetime module in Python. You may find more information about Gottumukkala and his other articles on his profile page.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.

Menu
Top