- How to convert a timedelta object into a datetime object in Python?
- Method 1: Using datetime.now()
- Method 2: Using datetime.today()
- Method 3: Using datetime.combine() with a date object and a time object
- Convert timedelta to datetime using datetime.combine()
- Method 4: Using the fromordinal() method and the toordinal() method
- Convert timedelta to datetime Object in Python (Example)
- Example Data & Imported Modules
- Example: Converting timedelta to datetime
- Video & Further Resources
- 2 Comments . Leave new
How to convert a timedelta object into a datetime object in Python?
In Python, a timedelta object represents a duration, whereas a datetime object represents a specific date and time. It is sometimes necessary to convert a timedelta into a datetime in order to perform certain operations or to manipulate the data in a specific way. There are several methods to do this conversion, and each has its advantages and disadvantages. Below are some methods to convert a timedelta into a datetime object in Python:
Method 1: Using datetime.now()
To convert a timedelta object into a datetime object in Python using the datetime.now() method, you can follow these steps:
td = datetime.timedelta(days=5, hours=3, minutes=30)
import datetime td = datetime.timedelta(days=10, hours=5, minutes=45) now = datetime.datetime.now() new_datetime = now - td print(new_datetime)
You can also add a timedelta object to a datetime object to get a new datetime object:
import datetime td = datetime.timedelta(days=5, hours=3, minutes=30) now = datetime.datetime.now() new_datetime = now + td print(new_datetime)
Method 2: Using datetime.today()
To convert a timedelta object into a datetime object in Python, we can use the datetime.today() method. Here’s how:
import datetime td = datetime.timedelta(days=7) now = datetime.datetime.today() new_datetime = now + td print(new_datetime)
In this example, we first create a timedelta object representing 7 days. Then, we get the current datetime using datetime.datetime.today() . We add the timedelta object to the current datetime using the + operator, which results in a new datetime object. Finally, we print the new datetime object.
We can also use the datetime.now() method instead of datetime.today() to get the current datetime with more precision. Here’s an example:
import datetime td = datetime.timedelta(hours=12) now = datetime.datetime.now() new_datetime = now + td print(new_datetime)
In this example, we create a timedelta object representing 12 hours. We get the current datetime using datetime.datetime.now() , which includes microseconds. We add the timedelta object to the current datetime and print the result.
That’s it! We can use the datetime.today() or datetime.now() methods to convert a timedelta object into a datetime object in Python.
Method 3: Using datetime.combine() with a date object and a time object
Convert timedelta to datetime using datetime.combine()
You can use the datetime.combine() method to convert a timedelta object to a datetime object. This method takes two arguments: a date object and a time object. You can create a date object using the date() method and a time object using the time() method.
from datetime import datetime, date, time, timedelta td = timedelta(days=2, hours=3, minutes=30) d = date.today() t = time(hour=12, minute=0) dt = datetime.combine(d, t) new_dt = dt + td print(new_dt)
In this example, we first create a timedelta object with a value of 2 days, 3 hours, and 30 minutes. We then create a date object using the today() method, which returns the current date. We also create a time object with a value of 12:00 PM using the time() method.
We then use the datetime.combine() method to combine the date and time objects into a datetime object. We add the timedelta object to the datetime object using the + operator to get the new datetime object.
The output of this code will be the new datetime object with the timedelta added to it.
You can also create a timedelta object by subtracting two datetime objects. Here’s an example:
from datetime import datetime, timedelta dt1 = datetime(2022, 1, 1) dt2 = datetime(2022, 1, 5) td = dt2 - dt1 print(td) new_dt = datetime.combine(dt1, datetime.min.time()) + td print(new_dt)
In this example, we create two datetime objects with a difference of 4 days. We subtract the first datetime object from the second to get a timedelta object. We then create a new datetime object using the datetime.combine() method and add the timedelta object to it.
The output of this code will be the new datetime object with the timedelta added to it.
Method 4: Using the fromordinal() method and the toordinal() method
To convert a timedelta object into a datetime object in Python using the fromordinal() method and the toordinal() method, follow these steps:
from datetime import datetime, timedelta
td = timedelta(days=10, hours=4, minutes=30, seconds=15)
dt = datetime.now() ordinal = dt.toordinal()
- Use the fromordinal() method to create a new datetime object by adding the ordinal value to the total seconds of the timedelta object:
new_dt = datetime.fromordinal(ordinal + td.days) + timedelta(seconds=td.seconds, microseconds=td.microseconds)
Here is the complete code:
from datetime import datetime, timedelta td = timedelta(days=10, hours=4, minutes=30, seconds=15) dt = datetime.now() ordinal = dt.toordinal() new_dt = datetime.fromordinal(ordinal + td.days) + timedelta(seconds=td.seconds, microseconds=td.microseconds) print(new_dt)
You can also create a function to convert any timedelta object into a datetime object using the same method:
def timedelta_to_datetime(td): dt = datetime.now() ordinal = dt.toordinal() new_dt = datetime.fromordinal(ordinal + td.days) + timedelta(seconds=td.seconds, microseconds=td.microseconds) return new_dt
You can then call this function with any timedelta object:
td = timedelta(days=5, hours=2, minutes=15) new_dt = timedelta_to_datetime(td) print(new_dt)
Convert timedelta to datetime Object in Python (Example)
In this Python tutorial you’ll learn how to convert timedelta objects to datetime objects.
The tutorial consists of one example for illustrating the transformation from timedelta objects to datetime objects. The content of the tutorial is structured as follows:
So now is the part you have been waiting for!
Example Data & Imported Modules
For this example, we need to import the datetime module and define a timedelta object, which is td in this case.
import datetime # Loading datetime module td = datetime.timedelta(days=45, seconds=500000) # instantiating timedelta object print(td) # printing the timedelta object # 50 days, 18:53:20
As seen in the previous output, td is formatted by the print() function automatically.
Let’s create an example now!
Example: Converting timedelta to datetime
Since timedelta objects refer to the diffference between two datetime objects, we need another datetime object as a reference. It is defined under the name dt_origin as follows.
dt_origin = datetime.datetime(1995, 5, 1, 14, 15, 3) # instantiating origin datetime object print(dt_origin) # printing the datetime object # 1995-05-01 14:15:03
Now we are ready to implement the conversion!
dt_new = dt_origin + td # generating a datetime from timedelta object print(dt_new) # printing the generated datetime object # 1995-06-21 09:08:23
Video & Further Resources
Have a look at the following video on my YouTube channel. In the video, I demonstrate the Python codes of this article.
The YouTube video will be added soon.
In addition, you may have a look at some of the other tutorials on statisticsglobe.com:
At this point you should have learned how to create datetime objects from timedelta objects in Python. Let me know in the comments, in case you have any further questions.
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.
2 Comments . Leave new
I’ve never commented anything in my whole life but i have to say this is the worst article that i have ever seen.
I’m even losing time to write this comment because your article doesn’t show anything AT ALL and yet it is still indexed as a relevant source to convert a timedelta to datetime. Next time if you take time to write an article, actually show what you wrote as a title instead of doing nothing.
Hi Gonzales, I’m sorry to hear that our article didn’t provide the information you are looking for. If you’d specify your problem in some more detail, we might be able to help. Regards,
Joachim