[Rails] Re: calculate users age

Jonas Elfström jonelf at gmail.com
Tue Jan 24 16:25:32 GMT 2006


On 1/24/06, Paul Barry <mail at paulbarry.com> wrote:
> I believe this would techincally not work in leap year, right?

Why not? yday is 1 to 366 in a leap year.
I tested a few dates in and out of leap years and it seems to work
just fine. If you find a case that breaks the functionality please
tell me. Maybe we should take it offlist...

--
/J

> On 1/24/06, Jonas Elfström <jonelf at gmail.com> wrote:
> >
> > On 1/24/06, Alan <alan at incrediblinc.com > wrote:
> > > Neatest method I found was posted by Justing in the forum, not so long
> > > ago :
> > >
> http://wrath.rubyonrails.org/pipermail/rails/2005-December/006145.html
> > >
> > > def age_at(date, dob)
> > >   day_diff = date.day - dob.day
> > >   month_diff = date.month - dob.month - (day_diff < 0 ? 1 : 0)
> > >   date.year - dob.year - (month_diff < 0 ? 1 : 0)
> > > end
> >
> > Sorry, could not help myself:
> >
> > def age_at(date, dob)
> >   date.year - dob.year - ( (date.yday-dob.yday) < 0 ? 1 : 0 )
> > end

--
Jonas Elfström


More information about the Rails mailing list