Last updated: Apr 8, 2024
Reading timeยท2 min
The Python "TypeError: descriptor 'date' for 'datetime.datetime' objects
doesn't apply to a 'int' object" occurs when we incorrectly try to use the
date
class.
To solve the error import the datetime
module and use the class as
datetime.date(2025, 11, 24)
.
Here is an example of how the error occurs.
import datetime # โ๏ธ TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'int' object d = datetime.datetime.date(2025, 11, 21)
We incorrectly used the date
class in the example.
The date
class is directly available in the datetime
module.
import datetime d = datetime.date(2025, 11, 21) print(d) # ๐๏ธ 2025-11-21
The
datetime.date()
class takes the year
, month
and day
as arguments.
Note that all arguments are required and must be in the following ranges:
MINYEAR
<= year <= MAXYEAR
If an argument is outside the specified ranges, a ValueError
is raised.
date
class from the datetime
moduleYou can also import the date
class from the datetime
module.
from datetime import date d = date(2025, 11, 21) print(d) # ๐๏ธ 2025-11-21
Instead of importing the entire datetime
module, we imported only the date
class.
This should be your preferred approach because it makes your code easier to read.
import datetime
, it is much harder to see which classes or methods from the datetime
module are being used in the file.It is also quite confusing that there is a datetime
class in the datetime
module.
import datetime # ๐๏ธ <module 'datetime' from '/usr/lib/python3.11/datetime.py'> print(datetime) # ๐๏ธ <class 'datetime.datetime'> print(datetime.datetime) # ๐๏ธ <class 'datetime.date'> print(datetime.date)
The first print() call prints the
datetime
module.
The next two calls print the datetime
and date
classes.
A module is a collection of classes and methods.
If you need to specify the hours, minutes and seconds, use the datetime
class.
from datetime import datetime d = datetime(2024, 11, 24, 9, 30, 0) print(d) # ๐๏ธ 2024-11-24 09:30:00
If you decide to directly import the date
class from the datetime
module,
make sure to not declare a date
variable in your code as that would override
the date
class.
You can learn more about the related topics by checking out the following tutorials: