• Skip to primary navigation
  • Skip to main content

The Programming Expert

Solving All of Your Programming Headaches

  • Home
  • Learn to Code
    • Python
    • JavaScript
  • Code Snippets
    • HTML
    • JavaScript
    • jQuery
    • PHP
    • Python
    • SAS
    • Ruby
  • About
  • Write for Us
You are here: Home / Python / Get Difference Between datetime Variables in Python

Get Difference Between datetime Variables in Python

March 7, 2022 Leave a Comment

To get the difference between two times using Python, subtract two dates just like you would subtract two numbers to get a datetime.timedelta object.

import datetime

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

difference_d2_d1 = datetime2 - datetime1

print(difference_d2_d1)

#Output:
3 days, 12:30:00

When working in Python, many times we need to create variables which represent dates and times. When creating and displaying values related to times, sometimes we need to display the difference between times in seconds, minutes, hours, or years.

We can easily show the time difference between date times.

To get the difference between two date times, you can subtract two times just like we would subtract two numbers.

After subtracting two times, we get a datetime.timedelta object.

Below is a simple example of how to get the time difference between two times in Python.

import datetime

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

difference_d2_d1 = datetime2 - datetime1

print(difference_d2_d1)

#Output:
3 days, 12:30:00

How to Get the Time Difference in Seconds Between Two Times in Python

We can take the example above and easily find the time difference between two times and convert it to number of seconds between the two times.

timedelta objects have many great functions, and to get the difference of two times in seconds, we can use the total_seconds() function.

Below is a simple example of how to get the time difference in seconds between two times in Python.

import datetime

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

difference_d2_d1 = datetime2 - datetime1

print(difference_d2_d1.total_seconds())

#Output:
304200.0

How to Get the Time Difference in Minutes Between Two Times in Python

We can take the example above and easily find the time difference between two times and convert it to minutes.

To find the difference in two times in minutes, we can divide the call to total_seconds() by 60.

Below is an example in Python of how to find the difference between two times in minutes.

import datetime

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

difference_d2_d1 = datetime2 - datetime1

print(difference_d2_d1.total_seconds()/60)

#Output:
5070.0

How to Get the Time Difference in Hours Between Two Times in Python

If you instead want to find the difference between two times in hours, you can divide the call to total_seconds() by 3600.

Below is an example in Python of how to find the difference between two times in hours.

import datetime

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

difference_d2_d1 = datetime2 - datetime1

print(difference_d2_d1.total_seconds()/3600)

#Output:
84.5

How to Get the Time Difference in Days Between Two Times in Python

We can take the example above and easily get the time difference between two times in days. To get the difference between two datetime objects in days, divide the call to total_seconds() by 86400.

Below is an example in Python of how to find the difference between two times in days.

import datetime

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

difference_d2_d1 = datetime2 - datetime1

print(difference_d2_d1.total_seconds()/86400)

#Output:
3.5208333333333335

To get the difference of two times in full days, we can access the ‘days’ attribute of the datetime.timedelta object.

Below is an example in Python of how to find the difference between two times in full days.

import datetime

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

difference_d2_d1 = datetime2 - datetime1

print(difference_d2_d1.days)

#Output:
3

Hopefully this article has been useful for you to use Python to find the time difference between two datetime objects.

Other Articles You'll Also Like:

  • 1.  Add Tuple to List in Python
  • 2.  Sort Series in pandas with sort_values() Function
  • 3.  pandas Drop Rows – Delete Rows from DataFrame with drop()
  • 4.  Keep Every Nth Element in List in Python
  • 5.  How to Draw a Triangle in Python Using turtle Module
  • 6.  List of Turtle Shapes in Python
  • 7.  Truncate String in Python with Slicing
  • 8.  How to Clear Turtle Screen in Python with clear() Function
  • 9.  Get Current Year in Python
  • 10.  Difference Between read(), readline() and readlines() in Python

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 *

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