Forum: Ruby on Rails calculate age based on DoB

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 Jeroen Houben (Guest)
on 2005-12-18 12:24
(Received via mailing list)
Hi,

I wrote a little helper that calulates someone's age based on his/her
date of birth.

   def age(dob)
     diff = Date.today - dob
     age = (diff / 365.25).floor
     age.to_s
   end

It works fine, but it's not completely accurate as it just takes the
average days in a year. It should be able to calculate this more
accurately, right? I can't work it out though..

Jeroen
450046d5f684ae5001c0b1a079d447d4?d=identicon&s=25 Sean Schertell (Guest)
on 2005-12-18 15:13
Jeroen Houben wrote:
> Hi,
>
> I wrote a little helper that calulates someone's age based on his/her
> date of birth.
>
>    def age(dob)
>      diff = Date.today - dob
>      age = (diff / 365.25).floor
>      age.to_s
>    end
>
> It works fine, but it's not completely accurate as it just takes the
> average days in a year. It should be able to calculate this more
> accurately, right? I can't work it out though..
>
> Jeroen


Give this a try Jeroen:

def get_age(dob)
  return nil unless dob
  now = Time.now
  now.year - dob.year - (dob.to_time.change(:year => now.year) > now ? 1
: 0)
end
9a46755c817121b0571a563e5ab02090?d=identicon&s=25 Brian Buckley (brian)
on 2005-12-18 16:06
(Received via mailing list)
>
>   >> now.year - dob.year - (dob.to_time.change(:year => now.year) > now ?
> 1


Nifty.  It can't handle dobs before 1970 though.
55742d912a9003080d202c5522d4de00?d=identicon&s=25 Ken Bowley (Guest)
on 2005-12-18 16:12
(Received via mailing list)
Here's one that's not optimized in any way, but it is accurate:

   def age(dob)
     unless dob.nil?
       a = Date.today.year - dob.year
       b = Date.new(Date.today.year, dob.month, dob.day)
       a = a-1 if b > Date.today
       return a
     end
     nil
   end
9a46755c817121b0571a563e5ab02090?d=identicon&s=25 Brian Buckley (brian)
on 2005-12-18 16:37
(Received via mailing list)
>
> >Here's one that's not optimized in any way, but it is accurate:


Good but fails on leap days.

d = Date.new(2000,2,29)
age(d)  #throws exception
450046d5f684ae5001c0b1a079d447d4?d=identicon&s=25 Sean Schertell (Guest)
on 2005-12-19 00:43
Brian Buckley wrote:
>>
>> >Here's one that's not optimized in any way, but it is accurate:
>
>
> Good but fails on leap days.
>
> d = Date.new(2000,2,29)
> age(d)  #throws exception

Ouch.  Okay so does anyone have a method for turning a date object into
an age which works for both dates before 1970 and leap days?
Eea7ad39737b0dbf3de38874e0a6c7d8?d=identicon&s=25 Justin Forder (Guest)
on 2005-12-19 03:48
(Received via mailing list)
Sean Schertell wrote:
>
> Ouch.  Okay so does anyone have a method for turning a date object into
> an age which works for both dates before 1970 and leap days?
>

Here's one for Brian to test:

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

Both arguments must be Dates.

regards

   Justin
B9a732fc30c32098347a0177c75ee27b?d=identicon&s=25 Jeroen Houben (Guest)
on 2005-12-19 10:10
(Received via mailing list)
Justin Forder wrote:
>>> d = Date.new(2000,2,29)
> 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
>
> Both arguments must be Dates.

Works fine for me. Also handles pre 1970 dates just fine.
Thanks!

Jeroen
998baab37c607466125c1235c1ad7006?d=identicon&s=25 Frank Rocco (frocco)
on 2005-12-19 15:50
(Received via mailing list)
What about this?

def age(dob)
    unless dob.nil?
      years = Date.today.year - dob.year
      if Date.today.month < dob.month
           years = years + 1
      end
      if (Date.today.month == dob.month and
          Date.today.day < dob.day)
               years = years - 1
      end
     return years
    end
    nil
   end

Frank
This topic is locked and can not be replied to.