[Rails] Re: calculate users age

Jonas Elfström jonelf at gmail.com
Tue Jan 24 12:29:42 GMT 2006


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

And now something for the swedish readers. Check out my validation of
swedish "social security number" in Ruby:
http://plea.se/me/validatePnum.html

--
Jonas Elfström


More information about the Rails mailing list