• 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 / Time Difference in Seconds Between Datetimes in Python

Time Difference in Seconds Between Datetimes in Python

March 7, 2022 Leave a Comment

To get the difference between two times in seconds using Python, we can use the total_seconds() function after subtracting two dates.

import datetime

datetime1 = datetime.datetime(2022,3,5,0,0,0)
datetime2 = datetime.datetime(2022,3,7,0,0,0)

difference_d2_d1 = datetime2 - datetime1

print(difference_d2_d1.total_seconds())

#Output:
172800.0

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 or hours.

We can easily show the time difference in seconds 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. 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 between two times in Python.

import datetime

datetime1 = datetime.datetime(2022,3,5,0,0,0)
datetime2 = datetime.datetime(2022,3,7,0,0,0)

difference_d2_d1 = datetime2 - datetime1

print(difference_d2_d1.total_seconds())

#Output:
172800.0

Time Difference in Minutes and Hours Using Python

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

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,7,0,0,0)

difference_d2_d1 = datetime2 - datetime1

print(difference_d2_d1.total_seconds()/60)

#Output:
2880.0

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,7,0,0,0)

difference_d2_d1 = datetime2 - datetime1

print(difference_d2_d1.total_seconds()/3600)

#Output:
48.0

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

Other Articles You'll Also Like:

  • 1.  Flatten List of Tuples in Python
  • 2.  Python Coin Flip – How to Simulate Flipping a Coin in Python
  • 3.  Split Column by Delimiter in pandas DataFrame
  • 4.  Python Check if Object Has Attribute
  • 5.  How to Remove Vowels from a String in Python
  • 6.  pandas mode – Find Mode of Series or Columns in DataFrame
  • 7.  Check if Variable is Float in Python
  • 8.  Remove Leading Zeros from String with lstrip() in Python
  • 9.  Get Last Character in String in Python
  • 10.  Combine Sets 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 *

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