How to find difference in years between two dates?

Bruno Desthuilliers onurb at xiludom.gro
Wed Jul 26 10:31:21 EDT 2006


John Machin wrote:
> Bruno Desthuilliers wrote:
> 
>>thebjorn wrote:
>>
>>>For the purpose of finding someone's age I was looking for a way to
>>>find how the difference in years between two dates, so I could do
>>>something like:
>>>
>>>  age = (date.today() - born).year
>>>
>>>but that didn't work (the timedelta class doesn't have a year
>>>accessor).
>>>
>>>I looked in the docs and the cookbook, but I couldn't find anything, so
>>>I came up with:
>>>
>>>  def age(born):
>>>      now = date.today()
>>>      birthday = date(now.year, born.month, born.day)
>>>      return now.year - born.year - (birthday > now and 1 or 0)
>>>
>>>i.e. calculate the "raw" years first and subtract one if he hasn't had
>>>his birthday yet this year... It works, but I'd rather use a standard
>>>and generic approach if it exists...?
>>
>>You may want to have a look at mxDatetime, which has a RelativeDateTime
>>type that seems to do what you want:
>>http://www.egenix.com/files/python/mxDateTime.html
>>
> 
> 
> Which pieces of the following seem to be working to you?

John, it seems you failed to notice the use of "may" and "seems" in my
post. IIRC, both are supposed to strongly suggest a lack of certitude.


-- 
bruno desthuilliers
python -c "print '@'.join(['.'.join([w[::-1] for w in p.split('.')]) for
p in 'onurb at xiludom.gro'.split('@')])"



More information about the Python-list mailing list