在Ruby中获取人的年龄

问题描述 投票:120回答:23

我想从一个人的生日那里得到一个人的年龄。 now - birthday / 365不起作用,因为有些年份有366天。我想出了以下代码:

now = Date.today
year = now.year - birth_date.year

if (date+year.year) > now
  year = year - 1
end

是否有更多Ruby的计​​算年龄的方法?

ruby-on-rails ruby
23个回答
402
投票

我知道我在这里参加派对的时间已经很晚了,但是当我试图计算出闰年2月29日出生的人的年龄时,接受的答案会非常糟糕。这是因为对birthday.to_date.change(:year => now.year)的调用创建了无效日期。

我使用以下代码(在Rails项目中):

def age(dob)
  now = Time.now.utc.to_date
  now.year - dob.year - ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? 0 : 1)
end

2
投票

这是this answer的转换(它收到了很多票):

# convert dates to yyyymmdd format
today = (Date.current.year * 100 + Date.current.month) * 100 + Date.today.day
dob = (dob.year * 100 + dob.month) * 100 + dob.day
# NOTE: could also use `.strftime('%Y%m%d').to_i`

# convert to age in years
years_old = (today - dob) / 10000

它的方法绝对是独一无二的,但是当你意识到它的作用时,它就非常有意义:

today = 20140702 # 2 July 2014

# person born this time last year is a 1 year old
years = (today - 20130702) / 10000

# person born a year ago tomorrow is still only 0 years old
years = (today - 20130703) / 10000

# person born today is 0
years = (today - 20140702) / 10000  # person born today is 0 years old

# person born in a leap year (eg. 1984) comparing with non-leap year
years = (20140228 - 19840229) / 10000 # 29 - a full year hasn't yet elapsed even though some leap year babies think it has, technically this is the last day of the previous year
years = (20140301 - 19840229) / 10000 # 30

# person born in a leap year (eg. 1984) comparing with leap year (eg. 2016)
years = (20160229 - 19840229) / 10000 # 32

1
投票

因为Ruby on Rails被标记,所以dotiw gem覆盖了Rails内置的distance_of_times_in_words并提供了可用于确定年龄的distance_of_times_in_words_hash。闰年在多年的部分处理得很好,但要注意2月29日确实会对需要理解的天数部分产生影响。此外,如果您不喜欢dotiw如何更改distance_of_time_in_words的格式,请使用:vague选项恢复为原始格式。

将dotiw添加到Gemfile:

gem 'dotiw'

在命令行上:

bundle

在相应的模型中包含DateHelper,以获取distance_of_time_in_words和distance_of_time_in_words_hash的访问权限。在此示例中,模型为“用户”,生日字段为“生日”。

class User < ActiveRecord::Base
  include ActionView::Helpers::DateHelper

将此方法添加到同一模型。

def age
  return nil if self.birthday.nil?
  date_today = Date.today
  age = distance_of_time_in_words_hash(date_today, self.birthday).fetch("years", 0)
  age *= -1 if self.birthday > date_today
  return age
end

用法:

u = User.new("birthday(1i)" => "2011", "birthday(2i)" => "10", "birthday(3i)" => "23")
u.age

1
投票

我相信这在功能上等同于@ philnash的答案,但IMO更容易理解。

class BirthDate
  def initialize(birth_date)
    @birth_date = birth_date
    @now = Time.now.utc.to_date
  end

  def time_ago_in_years
    if today_is_before_birthday_in_same_year?
      age_based_on_years - 1
    else
      age_based_on_years
    end
  end

  private

  def age_based_on_years
    @now.year - @birth_date.year
  end

  def today_is_before_birthday_in_same_year?
    (@now.month < @birth_date.month) || ((@now.month == @birth_date.month) && (@now.day < @birth_date.day))
  end
end

用法:

> BirthDate.new(Date.parse('1988-02-29')).time_ago_in_years
 => 31 

0
投票

以下似乎工作(但如果检查,我会很感激)。

age = now.year - bday.year
age -= 1 if now.to_a[7] < bday.to_a[7]

0
投票

如果你不关心一两天,这将是更短和相当自我解释。

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

0
投票

好的,这个:

def age
  return unless dob
  t = Date.today
  age = t.year - dob.year
  b4bday = t.strftime('%m%d') < dob.strftime('%m%d')
  age - (b4bday ? 1 : 0)
end

这假设我们使用rails,在模型上调用age方法,并且模型有一个日期数据库列dob。这与其他答案不同,因为此方法使用字符串来确定我们是否在今年的生日之前。

例如,如果dob是2004/2/28而today是2014/2/28,那么age将是2014 - 200410。花车将是02280229b4bday将是"0228" < "0229"true。最后,我们将从1中减去age并获得9

这将是比较两次的正常方式。

def age
  return unless dob
  t = Date.today
  age = today.year - dob.year
  b4bday = Date.new(2016, t.month, t.day) < Date.new(2016, dob.month, dob.day)
  age - (b4bday ? 1 : 0)
end

这工作原理相同,但b4bday线太长。 2016年也是不必要的。开头的字符串比较是结果。

你也可以这样做

Date::DATE_FORMATS[:md] = '%m%d'

def age
  return unless dob
  t = Date.today
  age = t.year - dob.year
  b4bday = t.to_s(:md) < dob.to_s(:md)
  age - (b4bday ? 1 : 0)
end

如果您不使用rails,请尝试此操作

def age(dob)
  t = Time.now
  age = t.year - dob.year
  b4bday = t.strftime('%m%d') < dob.strftime('%m%d')
  age - (b4bday ? 1 : 0)
end

👍🏼


0
投票

我认为不计数月会更好,因为你可以通过使用Time.zone.now.yday获得一年中的确切日期。

def age
  years  = Time.zone.now.year - birthday.year
  y_days = Time.zone.now.yday - birthday.yday

  y_days < 0 ? years - 1 : years
end

0
投票

想出了这个solution的Rails变种

def age(dob)
    now = Date.today
    age = now.year - dob.year
    age -= 1 if dob > now.years_ago(age)
    age
end

0
投票

DateHelper只能用于获取年份

puts time_ago_in_words '1999-08-22'

差不多20年了


-1
投票
  def birthday(user)
    today = Date.today
    new = user.birthday.to_date.change(:year => today.year)
    user = user.birthday
    if Date.civil_to_jd(today.year, today.month, today.day) >= Date.civil_to_jd(new.year, new.month, new.day)
      age = today.year - user.year
    else
      age = (today.year - user.year) -1
    end
    age
  end

49
投票

我发现这个解决方案运行良好,并且可供其他人阅读:

    age = Date.today.year - birthday.year
    age -= 1 if Date.today < birthday + age.years #for days before birthday

容易,您不必担心处理闰年等。


-1
投票
Time.now.year - self.birthdate.year - (birthdate.to_date.change(:year => Time.now.year) > Time.now.to_date ? 1 : 0)

-1
投票

考虑到闰年(并假设有效支持存在):

def age
  return unless birthday
  now = Time.now.utc.to_date
  years = now.year - birthday.year
  years - (birthday.years_since(years) > now ? 1 : 0)
end

years_since将正确地修改日期以考虑非闰年(当生日是02-29时)。


-1
投票

这是我的解决方案,它还允许计算特定日期的年龄:

def age on = Date.today
  (_ = on.year - birthday.year) - (on < birthday.since(_.years) ? 1 : 0)
end

-2
投票

我也必须处理这个问题,但数月之后。变得太复杂了。我能想到的最简单的方法是:

def month_number(today = Date.today)
  n = 0
  while (dob >> n+1) <= today
    n += 1
  end
  n
end

您可以在12个月内做同样的事情:

def age(today = Date.today)
  n = 0
  while (dob >> n+12) <= today
    n += 1
  end
  n
end

这将使用Date类来增加月份,这将处理28天和闰年等。


33
投票

用这个:

def age
  now = Time.now.utc.to_date
  now.year - birthday.year - (birthday.to_date.change(:year => now.year) > now ? 1 : 0)
end

16
投票

Ruby on Rails中的一个内核(ActiveSupport)。处理闰年,闰秒等等。

def age(birthday)
  (Time.now.to_s(:number).to_i - birthday.to_time.to_s(:number).to_i)/10e9.to_i
end

来自这里的逻辑 - Calculate age in C#

假设两个日期都在同一时区,如果没有在utc()之前调用to_s()


9
投票
(Date.today.strftime('%Y%m%d').to_i - dob.strftime('%Y%m%d').to_i) / 10000

6
投票

到目前为止的答案有点奇怪。您最初的尝试非常接近正确的方法:

birthday = DateTime.new(1900, 1, 1)
age = (DateTime.now - birthday) / 365.25 # or (1.year / 1.day)

您将得到一个小数结果,所以随意将结果转换为to_i的整数。这是一个更好的解决方案,因为它正确地将日期差异视为自事件以来以天(或相关时间类的情况下为秒)测量的时间段。然后根据一年中的天数进行简单划分即可得出年龄。以这种方式计算年龄时,只要保留原始DOB值,就不需要考虑闰年。


6
投票

我的建议:

def age(birthday)
    ((Time.now - birthday.to_time)/(60*60*24*365)).floor
end

诀窍是使用Time的减号操作返回秒


4
投票

我喜欢这一个:

now = Date.current
age = now.year - dob.year
age -= 1 if now.yday < dob.yday

4
投票

This answer是最好的,反而投票。


我喜欢@ philnash的解决方案,但有条件的可能更紧凑。布尔表达式的作用是使用lexicographic order比较[月,日]对,因此可以使用ruby的字符串比较:

def age(dob)
  now = Date.today
  now.year - dob.year - (now.strftime('%m%d') < dob.strftime('%m%d') ? 1 : 0)
end
© www.soinside.com 2019 - 2024. All rights reserved.