gpt4 book ai didi

python - dateNow = datetime.datetime.now() 导致 AttributeError : type object 'datetime.datetime' has no attribute 'datetime' "while calculating age

转载 作者:行者123 更新时间:2023-11-30 22:59:50 29 4
gpt4 key购买 nike

尝试找出如何将当前日期锁定为变量,以从输入的 self.birthday 中减去。我已经查看了各种示例和链接,但无济于事......建议?

from datetime import datetime
import time

class Person(object):
def __init__(self, name):
self.name = name
self.birthday = None


#getName returns the name of the person
def getName(self):
return self.name

#setBirthday sets their Birthday to a date
def setBirthday(self):
day = raw_input('Please enter the date of the month you were born on here ->')
month = raw_input('Please enter the month of the year you were born on here ->')
year = raw_input('Please enter the year you were born on here ->')
self.birthday = int(day), int(month), int(year)
print self.birthday

#getAge returns how many days old the individual is
def getAge(self):
dateNow = datetime.datetime.now()
dateBirth = self.birthday
timedelta = dateNow - dateBirth
timedelta = self.daysOld
print self.daysOld

错误消息是“dateNow = datetime.datetime.now()AttributeError:类型对象“datetime.datetime”没有属性“datetime””

我尝试过以下方法

datetime.date.today() 

没有成功

我正在使用的资源:

https://docs.python.org/3/library/datetime.html

https://pymotw.com/2/datetime/

最佳答案

datetime.datetime.now() 引用库(第一个 datetime)的类(第二个datetime)的方法(now) datetime),但是您没有导入整个库,而只是导入单个类(from datetime import datetime)。导入整个库或调用该类而不引用该库。

关于python - dateNow = datetime.datetime.now() 导致 AttributeError : type object 'datetime.datetime' has no attribute 'datetime' "while calculating age,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35587947/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com