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.
Furthermore, you may have a look at some of the other tutorials on this website:
- Add Days, Months & Years to datetime Object in Python
- Add Seconds, Minutes & Hours to datetime Object in Python
- Python Programming Tutorials
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.
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.
Thank you!
Welcome to the Statistics Globe newsletter. From now on, I’ll send you regular emails about statistics, data science, AI, and programming with R and Python.
I’m Joachim Schork. On this website, I provide statistics tutorials as well as code in Python and R programming.
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.
Thank you!
Please check your email inbox and click the confirmation link to complete your subscription. If you don’t see the email within a few minutes, please also check your spam/junk folder.







