litany against fear
Calculating Age in Rails
You’d think that this would be easy, but for some reason it wasn’t, at least for me. Let’s say you keep track of a User’s birthday with a date field. Great! Let’s show the user’s age.
Done! Right? WRONG. Worked fine for some users, until one of my
coworkers asked me…hey, you’re not 21 yet…how’d you magically gain a
year on your profile? Crap. Obviously this will work for everyone’s
whose birthday is BEFORE Date.today
, but not after.
So, we need a more exact method of calculating the age:
So what this method does instead is use the Rational
value
given by subtracting two Date
s, and divides it by the
number of days in a year. Since that number is still a
Rational
, calling floor
on it will round it
down to the nearest Fixnum
, giving us a (slightly more)
precise account of this user’s age. EDIT: But this is still
inaccurate!
Yeah, I thought it was easy too. Perhaps this shows that I need to test my app a little more thoroughly! It also would be nice to save the age in an instance level variable, but right now I don’t use the age more than once on a page so it doesn’t matter.
EDIT: It looks like my method is still not sufficient for leap years! The comments have posted MANY great solutions. My method is fine for a general estimation, but the comments have many solutions for that deal with greater accuracy. I’m honestly not sure which is best, so choose carefully when you’re developing your app.