Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

Troubleshooting "descriptor 'date' requires a 'datetime.datetime' object but received a 'int'"

Ask Question

In my code I ask the user for a date in the format dd/mm/yyyy .

currentdate = raw_input("Please enter todays date in the format dd/mm/yyyy: ")
day,month,year = currentdate.split('/')
today = datetime.date(int(year),int(month),int(day))

This returns the error

TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'int'

if I remove the int() then I end up with the same error only it says it received a 'str'

What am I doing wrong?

It seems that you have imported datetime.datetime module instead of datetime. This should work though:

import datetime
currentdate = raw_input("Please enter todays date in the format dd/mm/yyyy: ")
day,month,year = currentdate.split('/')
today = datetime.date(int(year),int(month),int(day))

..or this:

from datetime import date
currentdate = raw_input("Please enter todays date in the format dd/mm/yyyy: ")
day,month,year = currentdate.split('/')
today = date(int(year),int(month),int(day))

Explanation: In the first case you are effectively calling datetime.datetime.date(), a method on the object datetime in the module datetime. In the later case you create a new date() object with the constructor datetime.date().

Alternatively, you can change the import to:

from datetime import datetime, date

and then construct with date(y,m,d) (without the datetime. prefix).

Uh, what do you mean with “both”? You have access to the whole module content, when you use import datetime (e.g., what you mostly want, the datetime class there, under the name datetime.datetime). – Boldewyn Aug 3, 2018 at 7:01 Whoever organized this library in Python did not think it through. There's no way this isn't extremely confusing to inexperienced Python programmers. – womp Mar 20, 2020 at 5:18

I suspect that the datetime reference the object and not the module. You probably did have the following code (probably more complex):

from datetime import datetime
currentdate = raw_input("Please enter todays date in the format dd/mm/yyyy: ")
day,month,year = currentdate.split('/')
today = datetime.date(int(year),int(month),int(day))

You are thus calling the date method of the datetime class instead of calling the date function of the datetime module.

You can print the datetime object to see if this is really the case:

>>> import datetime
>>> print datetime
<module 'datetime' (built-in)>
>>> print datetime.date(1, 1, 1)
0001-01-01
>>> datetime = datetime.datetime
>>> print datetime
<type 'datetime.datetime'>
>>> print datetime.date(1, 1, 1)
Traceback (most recent call last):
  File "<pyshell#12>", line 1, in <module>
    print datetime.date(1, 1, 1)
TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'int'

TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'int'

This is because you have used variables like year, month, day.

Use something like this:

year1, month1, day1 =  [int(d) for d in startDate.split('-')]
print(date(year1, month1, day1))

and it will work.

The error suggest's your import looks fine. Instead, while doing an operation using datetime, make sure the values are converted to datetime format first.

use pandas.to_datetime to do the same, before you use any operation on the same.

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.