12 июня 2011 г.

Способ получения возраста по дате рождения

В рамках переноса самых замечательных постов моего старого бложика:
@property
def age(self):
today = date.today()
years = today.year - self.birth_date.year

if all((x >= y) for x, y in zip(today.timetuple(), self.birth_date.timetuple())):
age = years

else:
age = years - 1

return age


Еще вариант:
today = date.today()
years = today.year – self.birth_date.year
age = years – int(today.timetuple()[1:] < self.birth_date.timetuple()[1:])


И самый удачный:
birthday = date(1990, 01,23)
age = datetime.date.fromordinal(datetime.date.today().toordinal() – birthday.toordinal())

0 коммент.:

Отправить комментарий