dateNow = datetime.datetime.now()导致AttributeError:type object' datetime.datetime'没有属性' datetime'"在计算年龄时

时间:2016-02-23 20:53:56

标签: python datetime

试图找出如何将当前日期锁定为变量以从输入的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:type object' datetime.datetime'没有属性' datetime'"

我试过以下

datetime.date.today() 

没有成功

我使用的资源:

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

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

2 个答案:

答案 0 :(得分:3)

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

答案 1 :(得分:0)

尝试

n= date.today()

我会以字符串格式提供当前日期,然后使用split()