Pages

Sponsorship

40% off your First Order with GoDaddy.com!

Jan 2, 2010

Ruby: Extract Age from Birth Date


A Ruby function for extracting one's age by passing in the birth date as a parameter.

def birthdate_to_age(birthdate) #bDate must be in the date-time format
age = Date.today.year - birthdate.year
if Date.today.month < birthdate.month || (Date.today.month == birthdate.month && birthdate.day >= Date.today.day)
age = age - 1
end
age.to_s
end

No comments:

Post a Comment