Accessing python dateutil relativedelta values

I am very new to python and I ran into a small problem that I could not find an answer on 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

- two separate dates

years = diff_dates(d2,d1)

print "Years: ", years

      

The values ​​that have been printed for years are the correct values ​​that I expect. My problem is that I need to access these values ​​and compare them with some other values. Regardless of how I try to access the data, I get similar errors:

AttributeError: relativedelta instance has no __call__ method

      

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

+3


source to share


1 answer


The object that you call, that you call years

has all the information inside. This object has the required values ​​as attributes:



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

      

+3


source







All Articles