Convert timedelta to Integer in Python (Example)

 

In this Python tutorial, you’ll learn how to convert a timedelta object to an integer.

Table of contents:

Here’s the step-by-step process!

 

Example Data & Imported Modules

Let’s import the datetime module.

import datetime                                                       # Loading datetime module

We’ll also define a timedelta object that we can use in the following examples:

td = datetime.timedelta(days=56, seconds=62700, microseconds=479001)  # sample timedelta generation
print(td)                                                             # printing the sample timedelta
# 56 days, 17:25:00.479001

 

Example 1: Converting timedelta to Integer

To change the timedelta object to an integer, first, we can convert it to seconds and store the result as an integer. You can think of it as a timestamp.

td_in_sec = td.total_seconds()                                        # getting the total amount of time in seconds
print(td_in_sec)                                                      # printing total_seconds
# 4901100.479001

If the timedelta has a non-zero microsecond component, then we can simply convert the float number to an integer. If this is not the case, first, we should multiply the number by a million to include the decimal part in the integer part.

td_in_sec = td_in_sec * 1000000                                       # Combining the decimal part to the integer part
int_td = int(td_in_sec)                                               # Converting the number to an integer
print(int_td)                                                         # printing the integer
# 4901100479001

 

Transforming Integer to timedelta Back

If we want to transform an integer to a timedelta object back, then we need to do the operations in reverse order. See the following.

td_in_sec = int_td / 1000000                                          # Getting the time amount in seconds again
td_reverse = datetime.timedelta(seconds=td_in_sec)                    # re-creating the timedelta
print(td_reverse)                                                     # printing the re-created timedelta
# 56 days, 17:25:00.479001

 

Video & Further Resources

If you need further explanations on the Python code of this tutorial, I can recommend having a look at the following video on my YouTube channel. In the video, I illustrate the Python programming code of this article in Python:

 

The YouTube video will be added soon.

 

In addition, you might want to read the related articles on my homepage. Some posts can be found below:

 

You have learned in this tutorial how to transform timedelta to integer in Python programming. Please let me know in the comments section, in case you have any further questions or comments.

 

Ömer Ekiz Python Programming & Informatics

This page was created in collaboration with Ömer Ekiz. Have a look at Ömer’s author page to get further 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.

Top