排除故障“描述符'日期'需要一个'datetime.datetime'对象,但收到一个'int'". [英] Troubleshooting "descriptor 'date' requires a 'datetime.datetime' object but received a 'int'"
本文介绍了排除故障“描述符'日期'需要一个'datetime.datetime'对象,但收到一个'int'".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在我的代码中,我要求用户输入日期,格式为 dd/mm/yyyy
.
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))
这将返回错误
TypeError:描述符'date'需要一个'datetime.datetime'对象,但接收到一个'int'
TypeError: descriptor 'date' requires a 'datetime.datetime' object but received a 'int'
如果我删除了 int()
,那么我最终会遇到相同的错误,只是它说它收到了'str'
if I remove the int()
then I end up with the same error only it says it received a 'str'
我在做什么错了?
推荐答案
似乎您已导入了 datetime.datetime
模块,而不是 datetime
.但这应该可以工作:
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))
..或这个:
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))
这篇关于排除故障“描述符'日期'需要一个'datetime.datetime'对象,但收到一个'int'".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文