Hey Everyone,
does anyone have a good age in years calculation function for usage with
datetime.date that returns the age in nr of years of a person?
We were using:
def get_age(self):
now = datetime.today()
birthday = datetime(now.year, self.birthday.month, self.birthday.day)
return now.year - self.birthday.year - (birthday > now)
But the trouble is that this fails for people whose birthday falls on a
leap-day! ( bday = 1980-02-29 => birthday this year doesn't exist :-) )
For now I swaped to:
try:
birthday = datetime(now.year, self.birthday.month, self.birthday.day)
except ValueError:
birthday = datetime(now.year, self.birthday.month, self.birthday.day-1)
But that seems such a hack :-))
- bram
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Django users" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at
http://groups.google.com/group/django-users?hl=en
-~----------~----~----~----~------~----~------~--~---