This error occurs when you import the datetime class from the datetime module using
from datetime import datetime
and then try to call the timedelta
method like datetime.timedelta()
.
You can solve this error by removing the extra datetime when calling timedelta()
or use:
import datetime
instead of:
from datetime import datetime
This tutorial will go through the error in detail and how to solve it with code examples.
Table of contents
AttributeError: ‘datetime.datetime’ object has no attribute ‘timedelta’
AttributeError occurs in a Python program when we try to access an attribute (method or property) that does not exist for a particular object. datetime is a built-in Python module that supplies classes for manipulating dates and times. One of the classes in datetime is called datetime. It can be unclear when both the module and one of the classes share the same name. If you use the import syntax:
from datetime import datetime
You are importing the datetime
class, not the datetime
module. The class timedelta
is an attribute of the datetime module. We can verify this by using dir()
.
import datetime # dir of datetime module attributes = dir(datetime) print(attributes) print('timedelta' in attributes)
['MAXYEAR', 'MINYEAR', '__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', '__package__', '__spec__', 'date', 'datetime', 'datetime_CAPI', 'sys', 'time', 'timedelta', 'timezone', 'tzinfo'] True
So if you use the import syntax:
from datetime import timedelta
You can create a datetime object using the timedelta
class without using datetime.timedelta
.
Example
Consider the following example where we want to create a list of strings representing dates in mmddyyy
format. The dates will start on the current day and then go one week into the future.
First, we will get the current date using the now()
method. Then, we will create a list to store the strings and append the current date to the list.
The next step involves a for
loop over a range object and creating a datetime object for each day in the future.
For each datetime object, we call strftime to convert the object to a string representing the date then append the string to the list.
Let’s look at the code:
from datetime import datetime, timedelta today = datetime.now() dates = [] dates.append(today.strftime("%m%d%Y")) for i in range(0, 8): next_day = today + datetime.timedelta(days=i) dates.append(next_day.strftime("%m%d%Y")) print(dates)
Let’s run the code to see what happens:
--------------------------------------------------------------------------- AttributeError Traceback (most recent call last) Input In [7], in <cell line: 9>() 7 dates.append(today.strftime("%m%d%Y")) 9 for i in range(0, 7): ---> 11 next_day = today + datetime.timedelta(days=i) 13 dates.append(next_day.strftime("%m%d%Y")) 15 print(dates) AttributeError: type object 'datetime.datetime' has no attribute 'timedelta'
The error occurs because we imported the datetime
class. When we try to call timedelta
we are trying to call datetime.datetime.timedelta
, which does not exist.
Solution #1: Remove extra datetime
We can solve the error by removing the extra datetime
, as we have imported the timedelta
class.
from datetime import datetime, timedelta dates = [] today = datetime.now() dates.append(today.strftime("%m%d%Y")) for i in range(0, 8): next_day = today + timedelta(days=i) dates.append(next_day.strftime("%m%d%Y")) print(dates)
Let’s run the code to see the result:
['05192022', '05192022', '05202022', '05212022', '05222022', '05232022', '05242022', '05252022', '05262022']
We successfully created a list of strings representing days from a starting date to a week in the future.
Solution #2: Use import datetime
The second way to solve this error is to import the datetime module and then access the timedelta
constructor through datetime.timedelta()
. Let’s look at that the revised code:
import datetime dates = [] today = datetime.datetime.now() dates.append(today.strftime("%m%d%Y")) for i in range(0, 7): next_day = today + datetime.timedelta(days=i) dates.append(next_day.strftime("%m%d%Y")) print(dates)
Let’s run the code to see the result:
['05192022', '05192022', '05202022', '05212022', '05222022', '05232022', '05242022', '05252022']
We successfully created a list of strings representing days from a starting date to a week in the future.
Summary
Congratulations on reading to the end of this tutorial! Remember that from datetime import datetime
imports the datetime class and import datetime
imports the datetime module.
For further reading on AttributeErrors involving datetime, go to the articles:
- How to Solve Python AttributeError: ‘datetime.datetime’ has no attribute ‘datetime’
- How to Solve Python AttributeError: module ‘datetime’ has no attribute ‘now’
- How to Solve Python AttributeError: module ‘datetime’ has no attribute ‘strftime’
- How to Solve Python AttributeError: type object ‘datetime.datetime’ has no attribute ‘fromisoformat’
To learn more about Python for data science and machine learning, go to the online courses page on Python for the most comprehensive courses available.
Have fun and happy researching!
Suf is a senior advisor in data science with deep expertise in Natural Language Processing, Complex Networks, and Anomaly Detection. Formerly a postdoctoral research fellow, he applied advanced physics techniques to tackle real-world, data-heavy industry challenges. Before that, he was a particle physicist at the ATLAS Experiment of the Large Hadron Collider. Now, he’s focused on bringing more fun and curiosity to the world of science and research online.