简体   繁体   English

排除“描述符'日期'需要'datetime.datetime'对象,但收到'int'”

[英]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.

相关问题 描述符“日期”需要一个“ datetime.datetime”对象,但接收到一个“ int”,但这是一个日期吗? - Descriptor 'date' requires a 'datetime.datetime' object but received a 'int', but it is a date? TypeError: 描述符 'date' 需要一个 'datetime.datetime' 对象但收到了一个 'int' - TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'int' 描述符“日期”需要一个“日期时间.日期时间”object 但收到一个“系列”(Python) - Descriptor 'date' requires a 'datetime.datetime' object but received a 'Series' (Python) 描述符'date'需要'datetime.datetime'对象,但收到'unicode' - Descriptor 'date' requires a 'datetime.datetime' object but received a 'unicode' TypeError:描述符“日期”需要一个“日期时间.日期时间”object,但在从非英语翻译成英语时收到一个“int” - TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'int' when translation from Non english to english 描述符“日期”需要一个“ datetime.datetime”对象,但收到一个“ datetime.date” - descriptor 'date' requires a 'datetime.datetime' object but received a 'datetime.date' 类型错误:描述符“isoformat”需要一个“datetime.date”object 但收到一个“str” - TypeError: descriptor 'isoformat' requires a 'datetime.date' object but received a 'str' TypeError:描述符&#39;strftime&#39;需要&#39;datetime.date&#39;对象但收到&#39;Text&#39; - TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'Text' TypeError: 'datetime.datetime' 对象的描述符 'date' 不适用于 'int' object - TypeError: descriptor 'date' for 'datetime.datetime' objects doesn't apply to a 'int' object TypeError:描述符“ strftime”需要一个“ datetime.date”对象,但收到一个“ str” - TypeError: descriptor 'strftime' requires a 'datetime.date' object but received a 'str'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM