Convert datetime to timedelta Object in Python (2 Examples)

 

This article shows how to convert datetime to timedelta in the Python programming language.

Table of contents:

Let’s get started:

 

Creation of Example Data & Import of Modules

Firstly, we need to load the datetime module.

import datetime                                                                   # Loading the datetime module
dt1 = datetime.datetime(1990, 1, 15, 8, 25, 40)                                   # Generating sample datetime

 

Example 1: Convert datetime to timedelta Using a Constructor

In this section, I’ll illustrate how to generate a timedelta object with in-day time values. We can simply put the timedelta parameters into the timedelta constructor to achieve our goal.

td1 = datetime.timedelta(hours=dt1.hour, minutes=dt1.minute, seconds=dt1.second)  # timedelta construction
print(td1)                                                                        # printing timedelta
# 8:25:40

 

Example 2: Convert datetime to timedelta via Subtraction

Example 2 illustrates how to do the conversion between two datetime objects via subtraction. As the subtraction between two datetime objects returns a timedelta object, we can construct a second datetime object with the same date and with 0 hours, minutes and seconds. By subtracting the new object (dt2) from the old one (dt1), we can get a timedelta object with the same in-day time values as the datetime object.

dt2 = datetime.datetime(dt1.year, dt1.month, dt1.day, 0, 0, 0)                    # Creating another datetime
td2 = dt1 - dt2                                                                   # subtraction between two datetime objects
print(td2)                                                                        # printing timedelta
# 8:25:40

 

Video, Further Resources & Summary

I have recently released a video on my YouTube channel, which shows the Python programming syntax of this article. You can find the video below:

 

The YouTube video will be added soon.

 

In addition, you might want to have a look at the other posts on this website:

 

In summary: In this tutorial, I have demonstrated how to create a timedelta object from datetime in Python programming. If you have any additional questions, don’t hesitate to let me know in the comments. Furthermore, please subscribe to my email newsletter in order to receive regular updates on new articles.

 

Ö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.

Top