3

I am very new to python and I've run across a small problem that I haven't been able to find an answer to by googling. I am running the following code:

from dateutil import relativedelta as rdelta
def diff_dates(date1, date2):
    return rdelta.relativedelta(date1,date2)

d1 and d2 are two separate dates

years = diff_dates(d2,d1)

print "Years: ", years

The values that get printed out for years are the correct values that I'm expecting. My problem is that I need to access those values and compare against some other values. No matter how I try to access the data I get similar errors:

AttributeError: relativedelta instance has no __call__ method

I need to get the years, months, and days and any help would be greatly appreciated.

4

1 回答 1

3

你得到的,你调用的对象,years里面有全部信息。该对象具有您想要的值作为属性:

In [12]: d = rdelta.relativedelta(datetime.datetime(1998, 10, 20, 1, 2, 3), datetime.datetime(2001, 5, 3, 3, 4, 5))

In [13]: d
Out[13]: relativedelta(years=-2, months=-6, days=-14, hours=-2, minutes=-2, seconds=-2)

In [14]: d.years
Out[14]: -2

In [15]: d.months
Out[15]: -6
于 2014-09-09T14:36:36.543 回答