There isn't a solution to your particular problem.  Your problem is
that you are accepting invalid data.  Nobody can be born on a day that
doesn't exist.  I'd recommend verifying that the data be entered into
the database is valid.

On Aug 3, 5:37 am, Bram - Smartelectronix <[EMAIL PROTECTED]>
wrote:
> 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 django-users@googlegroups.com
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
-~----------~----~----~----~------~----~------~--~---

Reply via email to