Extract Age from Birth Date

Extract Age from Birth Date
A Ruby function for extracting one's age by passing in the birth date as a parameter.
1. def BirthdateToAge(bDate) #bDate must be in the date-time format
2.   age = Date.today.year - bDate.year
3.   if Date.today.month < bDate.month ||
4.       (Date.today.month == bDate.month && bDate.day >= Date.today.day)
5.     age = age - 1
6.   end
7.   age.to_s
8. end

Leave a Reply

Your email address will not be published. Required fields are marked *

Back To Top