Topic: Difference in years

I have each users birthday. Need to display how old they are in years. Anyone got a code sample?

Re: Difference in years

Try this:

(Time.now - birthday.to_time)/1.year

Railscasts - Free Ruby on Rails Screencasts

Re: Difference in years

Perfect. Thanks so much. How do I round to a whole number?

Re: Difference in years

((Time.now - birthday.to_time)/1.year).round

You can also use floor/ceil.

Railscasts - Free Ruby on Rails Screencasts

Re: Difference in years

You should be on the payroll. Thanks man.

So last issue is accurately calculating the users age. Right now it's off by 1 if the users birthday has already passed in the current year.

In that case I'll need to subtract 1. So to do that I need to compare the month/day portion of their birthday to the current date. As in:

unless (month/day part of birthday) > (month/day part of now)
  subtract 1
end

Feel like wasting any more of your time on a newb to show me how you'd do that?

This is actually the last issue I need to smooth out before I'm going to open the site to a few friends to test :-)

Re: Difference in years

Have you tried floor? This will round down:

((Time.now - birthday.to_time)/1.year).floor

If that doesn't work, perhaps you could provide an example of the input birthday and what result you are expecting.

Railscasts - Free Ruby on Rails Screencasts

Re: Difference in years

I wasn't aware of floor so no...I didn't try it.

Just did now...perfect and done. Still plenty of work to do, but I'm far enough along that I'm going to let people start playing with the site.

http://www.racebucket.com/home/show

Love to get your feedback, but be gentle...my first ROR site...and the CSS needs some...originality. It's a shameless, borrow/steal/copy job for now :-)

Re: Difference in years

Looks great! Feels very fast too, what host are you using?

BTW, if you needed to calculate the birthday to see if someone was old enough, try this:

if birthday.to_time > 13.years.ago
  # too young
else
  #...
end

Railscasts - Free Ruby on Rails Screencasts

Re: Difference in years

Hosting with TextDrive and I have no idea what I'm doing :-)

My other site is all ColdFusion and Fusebox: http://www.checkpointzero.com

I went to a ROR bootcamp a couple months ago and built most of RaceBucket in class. Been tinkering since. Signed up for TextDrive and spent the last few weeks figuring out how to get the site running on lighttpd without getting killed by their watchdog script due to memory leaks. Doing pretty good lately though with no load at all.

Just sent an email to about 25 folks to register and click around. We'll see how it holds up. Still need to do some work around form validation.