T
thebjorn
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...?
-- bjorn
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...?
-- bjorn