Age calculation


#1

Has anyone written some code to calculate a persons age based on their
date of birth?


#2

Hi James

I’ve been using this in my model. The field in the database is called
“birthday” creative I know :wink:

def age
the_date = Time.now
if the_date.month < birthday.month or
(the_date.month == birthday.month and the_date.day <
birthday.day )
the_date.year - birthday.year - 1
else
the_date.year - birthday.year
end
end

If anyone can see any issues with this please let me know.

thanx


#3

((Date.today - birthday).to_i)/365 will give you the age in years.

Hammed


#4

On 11/26/05, Hammed M. removed_email_address@domain.invalid wrote:

((Date.today - birthday).to_i)/365 will give you the age in years.

Much Nicer. Thanx

Hammed


#5

On Nov 25, 2005, at 3:31 AM, James Geary wrote:

Has anyone written some code to calculate a persons age based on
their date of birth?

Assuming #date_of_birth returns a Date object (or nil):

def age
dob = date_of_birth or return nil
now = Time.now
now.year - dob.year -
(dob.to_time.change(:year => now.year) > now ? 1 : 0)
end


Rob L.
removed_email_address@domain.invalid


#6

Warning: You’re going to get one off errors because of leap years.

Emmett