How to Calculate How Many Years Passed Since a Given Date in Ruby

How to calculate how many years passed since a given date in Ruby?

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)

How to calculate days passed since date

From "Count number of days between two dates" and assuming that created_at returns a Date object, it seems like you can use:

(Time.current.to_date - Record.created_at).to_i 

to get the difference in days

Calculate how many days since creation

Almost, but Ruby doesn't understand your way of writing a datetime.

require 'date'
(DateTime.now - DateTime.new(2018, 2, 16, 18, 40, 53, 0)).to_i

Count number of days between two dates

With the Date (and DateTime) classes you can do (end_date - start_date).to_i to get the number of days difference.

How can I find the number of days between two Date objects in Ruby?

Subtract the beginning date from the end date:

endDate - beginDate 

Ruby age method using date of birth including months

While this may be better posted to a codereview site, I will still give you my thoughts.

You have written a fairly long method for what could be a couple smaller ones.

First, I would write one method that takes the number of years an months and separate that into its own function.

def readable_age(years, months)
# for under 1 year olds
if years == 0
return months > 1 ? months.to_s + " months old" : months.to_s + " month old"

# for 1 year olds
elsif years == 1
return months > 1 ? years.to_s + " year and " + months.to_s + " months old" : years.to_s + " year and " + months.to_s + " month old"

# for older than 1
else
return months > 1 ? years.to_s + " years and " + months.to_s + " months old" : years.to_s + " years and " + months.to_s + " month old"
end
end

Though, if you do not mind adding some dependencies to your project, you can take advantage of the actionview gem, you can take advantage of the pluralize function. Something along the lines of

def readable_age(years, months)
year_text = ''
if years != 0
year_text = "#{years} #{pluralize('year', years)} and "
end

"#{year_text}#{pluralize('month', months)} old"
end

Now for your function to calculate the number of years and months.

def age(t)
dob = self.date_of_birth

months = (t.year * 12 + t.month) - (dob.year * 12 + dob.month)

# months / 12 will give the number of years
# months % 12 will give the number of months
readable_age(months / 12, months % 12)
end

EDIT

The reason I am passing a date object into the age function is to allow you to calculate the the age of a person for a given time stamp. It also makes it easier to test a function if it produces the same result given the same inputs.



Related Topics



Leave a reply



Submit