[英]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
. 在我的代码中,我要求用户输入格式为
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'
TypeError:描述符'date'需要'datetime.datetime'对象但收到'int'
if I remove the int()
then I end up with the same error only it says it received a 'str'
如果我删除
int()
然后我最终得到相同的错误只是它说它收到'str'
What am I doing wrong? 我究竟做错了什么?
It seems that you have imported datetime.datetime
module instead of datetime
. 您似乎已导入
datetime.datetime
模块而不是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))
Do you import like this? 你这样导入吗?
from datetime import datetime
Then you must change it to look like this: 然后你必须改变它看起来像这样:
import datetime
Explanation: In the first case you are effectively calling datetime.datetime.date()
, a method on the object datetime
in the module datetime
. 说明:在第一种情况下,您实际上正在调用
datetime.datetime.date()
,它是模块datetime
对象datetime
一个方法。 In the later case you create a new date()
object with the constructor datetime.date()
. 在后一种情况下,使用构造函数
datetime.date()
创建一个新的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). 然后用
date(y,m,d)
构造(没有datetime.
前缀)。
if you already have 如果你已经拥有
from datetime import datetime
then you can construct like so: 然后你可以像这样构造:
christmas = datetime(2013,12,25)
I can reproduce the error if I do 如果我这样做,我可以重现错误
from datetime import *
It goes away when I do 当我这样做时它会消失
import datetime
So check your imports. 所以检查你的进口。
You can use both datetime and datetime.datetime. 您可以使用datetime和datetime.datetime。 Write the imports like this:
写下这样的导入:
from datetime import datetime
import datetime as dt
time_1 = datetime.strptime('17:00:00', '%H:%M:%S')
time_1 = dt.time(time_1.hour, time_1.minute, time_1.second)
I suspect that the datetime
reference the object and not the module. 我怀疑
datetime
引用了对象而不是模块。 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. 因此,您调用
datetime
类的date
方法,而不是调用datetime
模块的date
函数。
You can print the datetime
object to see if this is really the case: 您可以打印
datetime
对象以查看是否确实如此:
>>> 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'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.