Module: DateAndTime::Calculations

Included in:
Date, Time
Defined in:
lib/general_time/core_ext/date_and_time/calculations.rb

Constant Summary collapse

AMOUNT_DAYS_PER_YEAR =
365
AMOUNT_WEEKS_PER_YEAR =
52
AMOUNT_MONTHS_PER_YEAR =
12

Instance Method Summary collapse

Instance Method Details

#days_between(date = ::Date.current) ⇒ Object

Returns the number of days to given date, default to Date.current.



8
9
10
11
12
# File 'lib/general_time/core_ext/date_and_time/calculations.rb', line 8

def days_between(date = ::Date.current)
  date1, date2 = self.to_date, date.to_date

  ((date2.yday - date1.yday) + AMOUNT_DAYS_PER_YEAR * (date2.year - date1.year)).abs
end

#months_between(date = ::Date.current) ⇒ Object

Returns the number of months to given date, default to Date.current.



22
23
24
25
26
# File 'lib/general_time/core_ext/date_and_time/calculations.rb', line 22

def months_between(date = ::Date.current)
  date1, date2 = self.to_date, date.to_date

  ((date2.month - date1.month) + AMOUNT_MONTHS_PER_YEAR * (date2.year - date1.year)).abs
end

#weeks_between(date = ::Date.current) ⇒ Object

Returns the number of weeks to given date, default to Date.current.



15
16
17
18
19
# File 'lib/general_time/core_ext/date_and_time/calculations.rb', line 15

def weeks_between(date = ::Date.current)
  date1, date2 = self.to_date, date.to_date

  ((date2.cweek - date1.cweek) + AMOUNT_WEEKS_PER_YEAR * (date2.year - date1.year)).abs
end

#years_between(date = ::Date.current) ⇒ Object

Returns the number of years to given date, default to Date.current.



29
30
31
32
33
# File 'lib/general_time/core_ext/date_and_time/calculations.rb', line 29

def years_between(date = ::Date.current)
  date1, date2 = self.to_date, date.to_date

  (date2.year - date1.year).abs
end