- Convert timedelta to seconds in Python
- A little insight about the problem in hand,
- Frequently Asked:
- Convert timedelta to seconds in python
- Related posts:
- Share your love
- Leave a Comment Cancel Reply
- Terms of Use
- Disclaimer
- Convert timedelta to Seconds in Python (Example)
- Example Data
- Example: Transform timedelta Object to Seconds Using total_seconds() Function
- Video, Further Resources & Summary
Convert timedelta to seconds in Python
This article will discuss how we can convert the datetime module’s timedelta object to total seconds in python.
A little insight about the problem in hand,
In the datetime module, the datetime.timedelta represents the difference between two dates, time, or datetime objects. For example, we have two timestamps as datetime objects,
from datetime import datetime time_1 = datetime(2020, 8, 1, 2, 10, 17, 100000) time_2 = datetime(2020, 8, 1, 3, 15, 12, 200002)
If you want the duration between these two timestamps, you can subtract these two datetime objects. It will give a timedelta i.e.
# Get different between two datetime as timedelta object. diff = (time_2 - time_1) print(diff) print( type(diff) )
This timedelta object contains the time difference between two datetime objects. But on printing it, we get the absolute difference in hh:mm::ss.ffffff format. What if, you want the absolute duration in total seconds only? For that we can convert this timedelta object to seconds only. Let’s see how to do that,
Frequently Asked:
Convert timedelta to seconds in python
The timedelta object represents a duration between to time points. It has a function total_seconds(), which gives the total seconds in the complete duration of timedelta object. Let’s use this function,
# Convert timedelta object to Seconds diff_in_seconds = diff.total_seconds() print("Total Time Difference : <> Seconds".format(diff_in_seconds) )
Total Time Difference : 3895.100002 Seconds
We got the difference between two timestamps in seconds only by converting timedelta to seconds.
But we got the seconds with decimal part too. If you interesed in only absolute number, then you can round off the value i.e.
# Convert timedelta object to Seconds diff_in_seconds = diff.total_seconds() # Round of the Seconds value diff_in_seconds = round(diff_in_seconds) print("Total Time Difference : <> Seconds".format(diff_in_seconds) )
Total Time Difference : 3895 Seconds
The complete working example is as follows,
from datetime import datetime time_1 = datetime(2020, 8, 1, 2, 10, 17, 100000) time_2 = datetime(2020, 8, 1, 3, 15, 12, 200002) # Get different between two datetime as timedelta object. diff = (time_2 - time_1) print(diff) print( type(diff) ) # Convert timedelta object to Seconds diff_in_seconds = diff.total_seconds() print("Total Time Difference : <> Seconds".format(diff_in_seconds) ) # Round of the Seconds value diff_in_seconds = round(diff_in_seconds) print("Total Time Difference : <> Seconds".format(diff_in_seconds) )
1:04:55.100002 Total Time Difference : 3895.100002 Seconds Total Time Difference : 3895 Seconds
Today we learned that how we can convert datetime.timedelta to seconds in python.
Related posts:
Share your love
Leave a Comment Cancel Reply
This site uses Akismet to reduce spam. Learn how your comment data is processed.
Terms of Use
Disclaimer
Copyright © 2023 thisPointer
To provide the best experiences, we and our partners use technologies like cookies to store and/or access device information. Consenting to these technologies will allow us and our partners to process personal data such as browsing behavior or unique IDs on this site and show (non-) personalized ads. Not consenting or withdrawing consent, may adversely affect certain features and functions.
Click below to consent to the above or make granular choices. Your choices will be applied to this site only. You can change your settings at any time, including withdrawing your consent, by using the toggles on the Cookie Policy, or by clicking on the manage consent button at the bottom of the screen.
The technical storage or access is strictly necessary for the legitimate purpose of enabling the use of a specific service explicitly requested by the subscriber or user, or for the sole purpose of carrying out the transmission of a communication over an electronic communications network.
The technical storage or access is necessary for the legitimate purpose of storing preferences that are not requested by the subscriber or user.
The technical storage or access that is used exclusively for statistical purposes. The technical storage or access that is used exclusively for anonymous statistical purposes. Without a subpoena, voluntary compliance on the part of your Internet Service Provider, or additional records from a third party, information stored or retrieved for this purpose alone cannot usually be used to identify you.
The technical storage or access is required to create user profiles to send advertising, or to track the user on a website or across several websites for similar marketing purposes.
Convert timedelta to Seconds in Python (Example)
This tutorial explains how to convert a timedelta object into seconds in the Python programming language.
The article will consist of these contents:
Let’s jump right to the tutorial!
Example Data
As a first step, we have to import the datetime module:
import datetime # Load datetime module
Let’s also construct some example data:
dt_1 = datetime.datetime(2023, 12, 5, 10, 30, 47) # Create first datetime object print(dt_1) # 2023-12-05 10:30:47
dt_2 = datetime.datetime(2022, 11, 9, 5, 38, 15) # Create second datetime object print(dt_2) # 2022-11-09 05:38:15
As you can see based on the previous outputs of the Python console, we have just created two datetime objects.
In the next step, I’m going to subtract those two dates and times from each other:
td = dt_1 - dt_2 # Subtract datetimes print(td) # Print timedelta object # 391 days, 4:52:32
As you can see, our new data object shows 391 days and 4 hours, 52 minutes, and 32 seconds.
Let’s check the data type of this data object:
print(type(td)) # Test data type of data object #
We have created a timedelta object.
Example: Transform timedelta Object to Seconds Using total_seconds() Function
The following Python code illustrates how to convert a timedelta object to seconds. For this task, we’ll use the total_seconds function:
td_sec = td.total_seconds() # Apply total_seconds function print(td_sec) # Print seconds # 33799952.0
As you can see, the corresponding seconds to our example timedelta object are shown in the previous output, i.e. 33799952 seconds.
Video, Further Resources & Summary
Have a look at the following video on my YouTube channel. In the video, I explain the contents of this article in Python.
The YouTube video will be added soon.
In addition, you may want to have a look at some of the other articles on this website. Some articles are listed below.
In summary: This page has demonstrated how to transform a timedelta object into seconds in Python. Don’t hesitate to tell me about it in the comments section if you have additional questions. In addition, please subscribe to my email newsletter for regular updates on the newest articles.
This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get more information about his professional background, a list of all his tutorials, as well as an overview on his other tasks on Statistics Globe.