Ruby_代码判断年龄是否大于13岁,判断生日是否符合规范

判断年龄是否大于13岁,判断生日是否符合

 

下面是ruby的代码, 如果是前端,就是js取出当前时间来,理论是一样的!!!!!!!


  def age_in_completed_years(birth_date, current_date)
    nr_of_years = current_date.year - birth_date.year
    (birth_date.month > current_date.month ||
    (birth_date.month >= current_date.month && birth_date.day >= current_date.day)) ? nr_of_years - 1 : nr_of_years
  end

  def validate_birth_date
    return errors.add(:birth_date, 'must be greater than 13 years') if age_in_completed_years(self.birth_date, Time.now) < 13
  end

 

你可能感兴趣的:(Ruby)