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

前端 未结 13 2242
被撕碎了的回忆
被撕碎了的回忆 2020-12-09 16:33

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 prob

13条回答
  •  青春惊慌失措
    2020-12-09 17:02

    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
    

提交回复
热议问题