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.
Jeroen H. (Guest)
on 2005-12-18 13: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
Sean S. (Guest)
on 2005-12-18 16:13
Jeroen H. 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
Brian B. (Guest)
on 2005-12-18 17: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.
Ken B. (Guest)
on 2005-12-18 17: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
Brian B. (Guest)
on 2005-12-18 17: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
Sean S. (Guest)
on 2005-12-19 01:43
Brian B. 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?
Justin F. (Guest)
on 2005-12-19 04:48
(Received via mailing list)
Sean S. 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
Jeroen H. (Guest)
on 2005-12-19 11:10
(Received via mailing list)
Justin F. 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
Frank R. (Guest)
on 2005-12-19 16: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.