I've just encountered this issue, and couldn't find a reasonable answer for it on the front page of Google. It's similar to this question asked in 2011, but for a newer version of Python, which results in a different error message.
What is causing these TypeErrors?
import datetime
my_date = datetime.datetime.date(2021, 3, 2)
Results in the error:
TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'int' object
Similarly, replacing the integers with strings also gives the same error:
import datetime
my_date = datetime.datetime.date("2021", "3", "2")
Gives:
TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'str' object
And using a list gives the same error:
import datetime
my_date = datetime.datetime.date([2021, 3, 2])
Results in:
TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'list' object
Similarly, using from datetime import datetime and datetime.date will result in the following error messages respectively:
TypeError: descriptor 'date' for 'datetime' objects doesn't apply to a 'int' object
TypeError: descriptor 'date' for 'datetime' objects doesn't apply to a 'str' object
TypeError: descriptor 'date' for 'datetime' objects doesn't apply to a 'list' object
import datetime
my_date = datetime.date(2021, 3, 2)
or
from datetime import date
my_date = date(2021, 3, 2)
The issue is that datetime.datetime.date() is a method on a datetime.datetime object. We were confusing the datetime module with the datetime.datetime class.
What we're really looking for is the datetime.date() constructor.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With