Module: Darthjee::CoreExt::Date

Included in:
Date
Defined in:
lib/darthjee/core_ext/date.rb

Instance Method Summary collapse

Instance Method Details

#days_between(other_date) ⇒ ::Integer

Calculates the number of days between 2 dates

Examples:

One year apart date

date = Date.new(2018, 11, 21)
other_date = Date.new(2019, 11, 21)

date.days_between(other_date)) # returns 365

Four year apart date (having a leap year)

date = Date.new(2018, 11, 21)
other_date = Date.new(2014, 11, 21)

date.days_between(other_date)) # returns 365 * 4 + 1 = 1461

Checking against time

date = Date.new(2018, 11, 21)
time = Time.new(2017, 11, 21, 12, 0, 0)

date.days_between(time)) # returns 365

Parameters:

  • other_date (::Date, ::Time)

    future/past date for comparisom

Returns:

  • (::Integer)

    days between two dates



28
29
30
# File 'lib/darthjee/core_ext/date.rb', line 28

def days_between(other_date)
  (self - other_date.to_date).abs
end