# Calculate age of a person and compare it

**URL:** <https://www.ruby-forum.com/t/calculate-age-of-a-person-and-compare-it/115186>\
**Category:** Rails\
**Created:** [September 21, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/calculate-age-of-a-person-and-compare-it/115186 "2007-09-21T13:15:34Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![abhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b487fb/32.png) [@abhi](https://www.ruby-forum.com/u/abhi)\
**Post date:** [September 21, 2007, 1:15pm UTC](https://www.ruby-forum.com/t/calculate-age-of-a-person-and-compare-it/115186/1 "2007-09-21T13:15:34Z")

</div>

how can i calculate the age of a person using database " birth date"

my teble is user and column is bday …  
now i want to calculate the age will some one please tell me how can i  
calculate the age…

actually i want to compare my two database fields birthdate and  
joining date …that is why i need this…

will someone please tell me how can i do that

---

<div class="post-metadata">

**Author:** ![abhi](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/b487fb/32.png) [@abhi](https://www.ruby-forum.com/u/abhi)\
**Post date:** [September 21, 2007, 1:19pm UTC](https://www.ruby-forum.com/t/calculate-age-of-a-person-and-compare-it/115186/2 "2007-09-21T13:19:21Z")

</div>

abhi wrote:

> how can i calculate the age of a person using database " birth date"
> 
> my teble is user and column is bday …  
> now i want to calculate the age will some one please tell me how can i  
> calculate the age…
> 
> actually i want to compare my two database fields birthdate and  
> joining date …that is why i need this…
> 
> will someone please tell me how can i do that

Does this help?

def age(dob)  
now = Time.now

# how many years?

# has their birthday occured this year yet?

# subtract 1 if so, 0 if not

age = now.year - dob.year - (dob.to\_time.change(:year =\> now.year) \>  
now ? 1 : 0)  
end
