Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 763
John mcnamara Avatar asked Apr 11 '11 09:04

John mcnamara


People also ask

How do I convert datetime to string in Python?

The strftime() method returns a string representing date and time using date, time or datetime object.

How do you create a time object in Python?

To create a time object, we use the time class in the datetime module using the statement, datetime. time(hour, minutes, seconds), where hour is the hour, minutes is the minutes, and seconds is the seconds.


1 Answers

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)) 
like image 112
plaes Avatar answered Oct 12 '22 18:10

plaes