Get person's age in Ruby

前端 未结 24 2966
忘了有多久
忘了有多久 2020-11-27 10:32

I\'d like to get a person\'s age from its birthday. now - birthday / 365 doesn\'t work, because some years have 366 days. I came up with the following code:

24条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-27 10:49

    If you don't care about a day or two, this would be shorter and pretty self-explanitory.

    (Time.now - Time.gm(1986, 1, 27).to_i).year - 1970
    

提交回复
热议问题