• 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
  • VBA
  • About
You are here: Home / Python / Convert timedelta to Seconds in Python

Convert timedelta to Seconds in Python

March 8, 2022 Leave a Comment

To convert a timedelta to seconds in Python, we can use the total_seconds() function on a timedelta object.

import datetime

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

timedelta_object = datetime2 - datetime1

print(timedelta_object.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 the difference between two times, it is useful to be able to easily convert to different units (seconds, minutes, hours, etc.)

We can easily convert a timedelta object in Python to seconds.

A timedelta object is created when you find the difference between two datetime objects in Python.

timedelta objects have many great functions, and convert a timedelta object to seconds, we can use the total_seconds() function.

Below is a simple example of how to convert a timedelta to seconds in Python.

import datetime

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

timedelta_object = datetime2 - datetime1

print(timedelta_object.total_seconds())

#Output:
172800.0

Convert timedelta to Minutes and Hours Using Python

We can take the example above and easily convert a timedelta object to minutes or hours.

To convert a timedelta object to minutes, we can divide the call to total_seconds() by 60.

Below is an example in Python of how to convert a timedelta to minutes.

import datetime

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

timedelta_object = datetime2 - datetime1

print(timedelta_object.total_seconds()/60)

#Output:
2880.0

If you instead want to convert a timedelta object to hours, you can divide the call to total_seconds() by 3600.

Below is an example in Python of how to convert a timedelta to hours.

import datetime

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

timedelta_object = datetime2 - datetime1

print(timedelta_object.total_seconds()/3600)

#Output:
48.0

Hopefully this article has been useful for you to use Python to convert a timedelta to seconds.

Other Articles You'll Also Like:

  • 1.  Write Variable to File Using Python
  • 2.  How to Check if Character is Uppercase in Python
  • 3.  Check if String is Date in Python
  • 4.  Get Public IP Address Using Python
  • 5.  Using Python to Add Trailing Zeros to String
  • 6.  Find Index of Maximum in List Using Python
  • 7.  Using Python to Initialize Array of Size N
  • 8.  Python nth Root – Find nth Root of Number with math.pow() Function
  • 9.  Python Trig – Using Trigonometric Functions in Python for Trigonometry
  • 10.  Creating a List of Zeros 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

The Programming Expert is a compilation of hundreds of code snippets to help you find solutions to your problems in Python, JavaScript, PHP, HTML, SAS, and more.

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 © 2022 · The Programming Expert · About · Privacy Policy