# Age of person - ActiveSupport

**URL:** <https://www.ruby-forum.com/t/age-of-person-activesupport/80288>\
**Category:** Rails\
**Created:** [December 23, 2006, 11:22am UTC](https://www.ruby-forum.com/t/age-of-person-activesupport/80288 "2006-12-23T11:22:09Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [December 23, 2006, 11:22am UTC](https://www.ruby-forum.com/t/age-of-person-activesupport/80288/1 "2006-12-23T11:22:09Z")

</div>

I’ve got a datetime column in my database for a person’s age. How would  
I be able to calculate the number of years they are from the DB date to  
current time? I’ve looked at ActiveSupport and tried a few ways but  
can’t figure it out. Anyone have any ideas on the best way to implement  
this?

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [December 23, 2006, 11:38am UTC](https://www.ruby-forum.com/t/age-of-person-activesupport/80288/2 "2006-12-23T11:38:37Z")

</div>

Hi Justin,

Where do you want to use this calculations?  
if you want to use it in a view, you can use the  
distance\_of\_time\_in\_words\_to\_now.

if you want to use it in a controller, well this is a problem i have  
also… all the text helpers does not work with the an ActiveSupport  
elements.

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [December 23, 2006, 2:35pm UTC](https://www.ruby-forum.com/t/age-of-person-activesupport/80288/3 "2006-12-23T14:35:20Z")

</div>

I tried the ‘time\_ago\_in\_words’ helper method but it comes up as ‘over 4  
years’ when what I need is the approximate number of years. Any idea on  
how to get the method to display the exact number(integer) of years?

Thanks for the help!

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [December 23, 2006, 6:14pm UTC](https://www.ruby-forum.com/t/age-of-person-activesupport/80288/4 "2006-12-23T18:14:42Z")

</div>

Aw, works perfect. Thanks!

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [December 23, 2006, 2:45pm UTC](https://www.ruby-forum.com/t/age-of-person-activesupport/80288/5 "2006-12-23T14:45:20Z")

</div>

Justin Ko wrote:

> I’ve got a datetime column in my database for a person’s age. How would  
> I be able to calculate the number of years they are from the DB date to  
> current time? I’ve looked at ActiveSupport and tried a few ways but  
> can’t figure it out. Anyone have any ideas on the best way to implement  
> this?

In my Rails app I do it like this:

def age  
((Time.now - birthday)/(60_60_24)/365.2422).to\_i  
end

Cheers,  
JÃ¸rgen

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [December 24, 2006, 1:23am UTC](https://www.ruby-forum.com/t/age-of-person-activesupport/80288/6 "2006-12-24T01:23:25Z")

</div>

Elad M. - [Creopolis.com](http://Creopolis.com) wrote:

> this will not work?
> 
> def age (time)  
> return (DateTime.now - time).years  
> end

Nope. Can you?

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [December 23, 2006, 11:28pm UTC](https://www.ruby-forum.com/t/age-of-person-activesupport/80288/7 "2006-12-23T23:28:04Z")

</div>

this will not work?

def age (time)  
return (DateTime.now - time).years  
end

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [December 24, 2006, 6:22am UTC](https://www.ruby-forum.com/t/age-of-person-activesupport/80288/8 "2006-12-24T06:22:57Z")

</div>

Justin Ko wrote:

> Posted via [http://www.ruby-forum.com/](http://www.ruby-forum.com/).  
> This is probably overkill if this is the only unit based math you want  
> to do but…

gem install ruby-units

require ‘ruby-units’  
age = ‘years’.since(birthday) # where birthday is either a Time,  
DateTime, or string of a date

if you install and require the ‘Chronic’ gem, you can pass all sorts of  
funky stuff…

require ‘chronic’  
‘days’.until ‘monday’ #=\> 1.48601 days

\_Kevin

---

<div class="post-metadata">

**Author:** ![jkay](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/b4bc9f/32.png) [@jkay](https://www.ruby-forum.com/u/jkay)\
**Post date:** [December 24, 2006, 10:20am UTC](https://www.ruby-forum.com/t/age-of-person-activesupport/80288/9 "2006-12-24T10:20:23Z")

</div>

Yes, for now this is the only unit based math I need. I’ll definately  
remember those gems for future features though. Thanks!
