Class: Date
- Inherits:
-
Object
- Object
- Date
- Defined in:
- lib/sixarm_ruby_date_age.rb
Instance Method Summary collapse
-
#age_in_days(compare_date = Date.today) ⇒ Integer
(also: #age_in_days_on)
The age in days for a given date.
-
#age_in_years(compare_date = Date.today) ⇒ Integer
(also: #age_in_years_on)
The age in years for a given date.
Instance Method Details
#age_in_days(compare_date = Date.today) ⇒ Integer Also known as: age_in_days_on
Returns the age in days for a given date.
30 31 32 33 |
# File 'lib/sixarm_ruby_date_age.rb', line 30 def age_in_days(compare_date = Date.today) (compare_date.is_a? Date) or raise ArgumentError, "compare_date must be a date" (compare_date - self).to_i end |
#age_in_years(compare_date = Date.today) ⇒ Integer Also known as: age_in_years_on
Returns the age in years for a given date.
61 62 63 64 65 66 67 68 |
# File 'lib/sixarm_ruby_date_age.rb', line 61 def age_in_years(compare_date = Date.today) (compare_date.is_a? Date) or raise ArgumentError, "compare_date must be a date" return 0 if self == compare_date age = compare_date.year - self.year min, max = [self, compare_date].sort age -= 1 if max.month < min.month or (max.month == min.month and max.day < min.day) age end |