Has anyone written some code to calculate a persons age based on their
date of birth?
Hi James
I’ve been using this in my model. The field in the database is called
“birthday” creative I know
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
((Date.today - birthday).to_i)/365 will give you the age in years.
Hammed
On 11/26/05, Hammed M. [email protected] wrote:
((Date.today - birthday).to_i)/365 will give you the age in years.
Much Nicer. Thanx
Hammed
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.
[email protected]
Warning: You’re going to get one off errors because of leap years.
Emmett