加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

使用出生日期的ruby年龄方法,包括月份

发布时间:2020-12-17 03:56:54 所属栏目:百科 来源:网络整理
导读:我的用户模型上有一个方法来计算用户的年龄并返回一个人类可读的字符串.我的用户可以在1个月及以上之间,因此返回的字符串会有所不同,具体取决于该人是“2个月大”还是“1岁”或“2岁3个月”. 我已经回顾了几个SO帖子来解决这个问题.有什么我想念的吗?闰年?
我的用户模型上有一个方法来计算用户的年龄并返回一个人类可读的字符串.我的用户可以在1个月及以上之间,因此返回的字符串会有所不同,具体取决于该人是“2个月大”还是“1岁”或“2岁3个月”.

我已经回顾了几个SO帖子来解决这个问题.有什么我想念的吗?闰年?谢谢!

def age
    dob = self.date_of_birth

    # if a date of birth is not nil
    if dob != nil 

      # get current date  
      now = Date.current

      # has person had their birthday yet this year
      had_birthday = ((now.month > dob.month || (now.month == dob.month && now.day >= dob.day)) ? true : false) 

      # if yes then subtract this year from birthday year,if not then also subtract 1 to get how many full years old they are
      years = now.year - dob.year - (had_birthday ? 0 : 1)

      # get the calendar month difference from birthdya calendar month and today's calendar month.  if they have not had their birthdya yet then subtract the difference from 12
      months = had_birthday ? now.month - dob.month : 12 - (now.month - dob.month)

      # 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

    # No date of birth saved so can not calculate age
    else
      return "No Date of Birth"
    end
  end

解决方法

虽然这可能更好地发布到 codereview网站,我仍然会给你我的想法.

你已经为一些较小的方法编写了一个相当长的方法.

首先,我会写一个方法,花费一个月的年数,并将其分成自己的函数.

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

但是,如果您不介意在项目中添加一些依赖项,则可以利用actionview gem,您可以利用pluralize函数.有点像

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

现在为您的函数计算年数和月数.

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,15 % 12)
end

编辑

我将日期对象传递给年龄函数的原因是允许您计算给定时间戳的人的年龄.如果在给定相同输入的情况下产生相同的结果,它还可以更容易地测试函数.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读