views:

560

answers:

7

This question was here for other languages, so let here be one for Ruby.

How do I calculate number of complete years that have passed from a given date? As you probably have guessed, that's to calculate person's age automatically. The closest one is distance_of_time_in_words Rails helper, so the following template

Jack is <%= distance_of_time_in_words (Time.now, Time.local(1950,03,22)) %> old.

yields

Jack is over 59 years old.

But I need more precise function that yields just number. Is there one?

If there exists some kind of Ruby on Rails helper function for this, this is OK, although pure Ruby solution would be better.

Edit: the gist of the question is that a non-approximate solution is needed. At the 2nd of March Jack should be 59 years old and the next day he should be 60 years old. Leap years and such should be taken into account.

A: 
require 'date'

def years_since(dt)
    delta = (Date.today - Date.parse(dt)) / 365
    delta.to_i
end
sh-beta
A: 

How about something like:

def years_diff(from_time,to_time)
  (((to_time - from_time).abs)/ (365 * 24 * 60 * 60)).to_i
end

years_diff(Time.now,Time.local(1950,03,22)) #=> 59
years_diff(Time.now,Time.local(2009,03,22)) #=> 0
years_diff(Time.now,Time.local(2008,03,22)) #=> 1
khelll
This won't going to work when the current date is near birthday (leap years!), and for a person of more than 1460 years old it's going to yield ever increasing errors.
Pavel Shved
+11  A: 

Do you want age as people typically understand it, or are you looking for a precise measure of time elapsed? If the former, there is no need to worry about leap years and other complications. You simply need to compute a difference in years and reduce it if the person has not had a birthday yet this year. If the latter, you can convert seconds elapsed into years, as other answers have suggested.

def age_in_completed_years (bd, d)
    # Difference in years, less one if you have not had a birthday this year.
    a = d.year - bd.year
    a = a - 1 if (
         bd.month >  d.month or 
        (bd.month >= d.month and bd.day > d.day)
    )
    a
end

birthdate = Date.new(2000, 12, 15)
today     = Date.new(2009, 12, 14)

puts age_in_completed_years(birthdate, today)
FM
It seems that not have you only answered the question, but also formulated the question in a better way :-)
Pavel Shved
+1, but what if I was born on, say Date.new(1980, 2, 29) ? ;-)
Mike Woodhouse
@Mike Bummer -- no birthday for you. Good point. Fixed the code.
FM
Telemachus
@Telemachus Indeed! Old Man Leap Year is a persistent bastard.
FM
+2  A: 

I have a gem/plugin called dotiw that has a distance_of_time_in_words_hash that will return a hash like: { :years => 59, :months => 11, :days => 27 }. From that you could work out if it's near a certain limit.

Ryan Bigg
+1  A: 

Same idea as FM but with a simplified if statement. Obviously, you could add a second argument instead of using current time.

def age(birthdate)
  now = DateTime.now
  age = now.year - birthdate.year
  age -= 1 if(now.yday < birthdate.yday)
  age
end
`yday` is a cool funciton, thanks. Unfortunately, It will handle leap years differently, if the person is born later than in February.
Pavel Shved
+1  A: 

I think this will always work, even for someone with a birthday near a leap day:

require 'date'

def calculate_age(start_date, end_date)
  end_date.year - start_date.year - ((end_date.month > start_date.month || (end_date.month == start_date.month && end_date.day >= start_date.day)) ? 0 : 1)
end

puts calculate_age( Date.strptime('03/02/1968', '%m/%d/%Y'), Date.strptime('03/02/2010', '%m/%d/%Y'))

The calculated age with this method in the example call above is 42, which is correct despite 1968 being a leap year and the birthday being near a leap day.

Plus, this way there is no need to create a local variable.

Glenn
A: 

withing http://github.com/radar/dotiw

Jack is <%= distance_of_time_in_words (Time.now, Time.local(1950,03,22)) %> old.

produce

Jack is 60 years old
lda