George Baker
George Baker

Reputation: 31

Accessing python dateutil relativedelta values

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.

Upvotes: 3

Views: 1500

Answers (1)

matiasg
matiasg

Reputation: 2019

The object you get, that you call years, has the whole information inside. That object has as attributes the values you want:

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

Upvotes: 3

Related Questions