How to find difference in years between two dates?

thebjorn BjornSteinarFjeldPettersen at gmail.com
Wed Jul 26 17:00:10 EDT 2006


Bruno Desthuilliers wrote:
[...]
> Possible solution:
>
> import mx.DateTime as dt
> def age(date):
>     return dt.Age(dt.today(), date).years
> born = dt.Date(1967, 5, 1)
> assert age(born) == 39

dealbreaker:

>>> age(datetime.date(1970,5,2))
Traceback (most recent call last):
  File "<stdin>", line 1, in ?
  File "c:\python24\lib\site-packages\mx\DateTime\DateTime.py", line
842, in RelativeDateTimeDiff
    diff = date1 - date2
TypeError: unsupported operand type(s) for -: 'DateTime' and
'datetime.date'

I'm getting data from a database, and conversions are out of the
question for something like this. Otherwise it's a fine library :-)

-- bjorn




More information about the Python-list mailing list