Descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'int' object

avatar
Borislav Hadzhiev

Last updated: Apr 8, 2024
2 min

banner

# Descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'int' object

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).

typeerror descriptor date for datetime datetime objects

Here is an example of how the error occurs.

main.py
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.

main.py
import datetime d = datetime.date(2025, 11, 21) print(d) # ๐Ÿ‘‰๏ธ 2025-11-21

use date class from datetime module

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
  • 1 <= month <= 12
  • 1 <= day <= number of days in the given month and year

If an argument is outside the specified ranges, a ValueError is raised.

# Importing the date class from the datetime module

You can also import the date class from the datetime module.

main.py
from datetime import date d = date(2025, 11, 21) print(d) # ๐Ÿ‘‰๏ธ 2025-11-21

import date class from datetime module

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.

For example, when we use an import such as 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.

main.py
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.

main.py
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.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.

Copyright ยฉ 2024 Borislav Hadzhiev