RE: calculate age based on DoB

What about this:

def age
Time.now.year - date_of_birth.year - (
((date_of_birth.month * 31 + date_of_birth.day > Time.now.month * 31

  • Time.now.day) and Time.now.year != date_of_birth.year) ? 1 : 0
    ) unless date_of_birth.blank?
    end

That should work I think.

-Jonathan.

Jonathan V. wrote:

What about this:

def age
Time.now.year - date_of_birth.year - (
((date_of_birth.month * 31 + date_of_birth.day > Time.now.month * 31

  • Time.now.day) and Time.now.year != date_of_birth.year) ? 1 : 0
    ) unless date_of_birth.blank?
    end

That should work I think.

It seems people are now 1 year younger than they actually are. So one
could claim this is the best implementation yet.

J

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs