Datetime.Datetime Has No Attribute Datetime

type object 'datetime.datetime' has no attribute 'datetime'

For python 3.3

from datetime import datetime, timedelta
futuredate = datetime.now() + timedelta(days=10)

How to solve "type object 'datetime.datetime' has no attribute 'timedelta'" when creating a new date?

You've imported the wrong thing; you've done from datetime import datetime so that datetime now refers to the class, not the containing module.

Either do:

import datetime
...article.created_on + datetime.timedelta(...)

or

from datetime import datetime, timedelta
...article.created_on + timedelta(...)

Python's datetime: "AttributeError: type object 'datetime.date' has no attribute 'strptime'"

There indeed is no strptime() for a datetime.date object, and there is no strpdate() function either.

Just use datetime.strptime() and convert to a date object afterwards:

my_date = datetime.datetime.strptime(date_str, "%d-%m-%Y").date()

type object 'datetime.datetime' has no attribute 'fromisoformat'

The issue here is actually that fromisoformat is not available in Python versions older than 3.7, you can see that clearly stated in the documenation here.

Return a date corresponding to a date_string given in the format YYYY-MM-DD:
>>>

>>> from datetime import date
>>> date.fromisoformat('2019-12-04')
datetime.date(2019, 12, 4)

This is the inverse of date.isoformat(). It only supports the format YYYY-MM-DD.

New in version 3.7.

AttributeError while using timedelta: type object 'datetime.datetime' has no attribute 'datetime'

You are importing datetime from datetime. Later in the code you using datetime.datetime, so that's giving you error.

You shoud just call t = datetime(*struct_time[:6])

or

just do import datetime and call it t = datetime.datetime(*struct_time[:6])

The correct program should look like:

import time
import datetime
time_string = '2018-07-16T23:50:55+0000'

#Reduct 8 hours and print in human readable format
struct_time = time.strptime(time_string, "%Y-%m-%dT%H:%M:%S+0000")
t = datetime.datetime(*struct_time[:6])
delta = datetime.timedelta(hours=8)
print(t+delta)


Related Topics



Leave a reply



Submit