type object ‘datetime.datetime’ has no attribute ‘timedelta’

AttributeError: type object ‘datetime.datetime’ has no attribute ‘timedelta’ message is generated by Python when you try to access the timedelta attribute of a datetime object. The timedelta attribute is used to calculate the difference between two datetime objects, so it is not available for datetime objects that do not have a timestamp.

 

I think you are doing something like this

#import datetime 
from datetime import datetime

tomorrowdate = datetime.now() + datetime.timedelta(days=1)

 

And you are getting this AttributeError: type object ‘datetime.datetime’ has no attribute ‘timedelta’

type object datetime.datetime has no attribute timedelta

 

 

We can solve this AttributeError: type object ‘datetime.datetime’ has no attribute ‘timedelta’ by importing timedelta because timedelta is not a method under datetime class that you are trying to call it using datetime object instead we have to import it.

 

Complete Program

#import classes timedelta and datetime from datetime package
from datetime import datetime, timedelta

#we can directly call timedelta 
tomorrowdate = datetime.now() + timedelta(days=1)

print(tomorrowdate)

 

Output:

2022-07-02 15:13:31.636876

 

I hope your error is solved. Also, read more about python.

 

Share this post

Leave a Reply

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