Convert timedelta to Days in Python (Example)


In this article, I’ll illustrate how to convert timedelta objects to days in Python programming.

The article looks as follows:

Let’s just jump right in.


Exemplifying Data & Loaded Modules

Let’s import the datetime module.

import datetime                                    # Loading datetime module

Let’s also create a timedelta object in Python:

td = datetime.timedelta(days=75, seconds=54321)    # generating sample timedelta
print(td)                                          # printing timedelta
# 75 days, 15:05:21.600300


Example: Calculate Equivalent Time in Days

The following code demonstrates how to express a timedelta object in terms of days.

seconds_in_day = 60 * 60 * 24                      # setting second count in a day
day_count = td.days + td.seconds/seconds_in_day    # calculating total time in days
print(day_count)                                   # printing the result
# 75.62872222569443

We can reconstruct the timedelta object in order to confirm our result.

td_days = datetime.timedelta(days=day_count)       # timedelta recreation
print(td_days)                                     # printing the verification timedelta
# 75 days, 15:05:21.600300

As seen, it is identical to the initial timedelta object td.


Video & Further Resources

Have a look at the following video on my YouTube channel. In the video, I explain the Python programming codes of this article in the Python programming language.


The YouTube video will be added soon.


Furthermore, you might want to have a look at the other articles on this homepage.


You have learned in this tutorial how to express timedelta objects in days in the Python programming language. Please let me know in the comments section, in case you have additional questions.


Ömer Ekiz Python Programming & Informatics

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.


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.

Leave a Reply

Your email address will not be published. Required fields are marked *

Fill out this field
Fill out this field
Please enter a valid email address.