module DateAndTime::Calculations
def after?(date_or_time)
def after?(date_or_time) self > date_or_time end
def all_day
def all_day beginning_of_day..end_of_day end
def all_month
def all_month beginning_of_month..end_of_month end
def all_quarter
def all_quarter beginning_of_quarter..end_of_quarter end
def all_week(start_day = Date.beginning_of_week)
Returns a Range representing the whole week of the current date/time.
def all_week(start_day = Date.beginning_of_week) beginning_of_week(start_day)..end_of_week(start_day) end
def all_year
def all_year beginning_of_year..end_of_year end
def before?(date_or_time)
def before?(date_or_time) self < date_or_time end
def beginning_of_month
now = DateTime.current # => Thu, 18 Jun 2015 15:23:13 +0000
+DateTime+ objects will have a time set to 0:00.
today.beginning_of_month # => Mon, 01 Jun 2015
today = Date.today # => Thu, 18 Jun 2015
Returns a new date/time at the start of the month.
def beginning_of_month first_hour(change(day: 1)) end
def beginning_of_quarter
now = DateTime.current # => Fri, 10 Jul 2015 18:41:29 +0000
+DateTime+ objects will have a time set to 0:00.
today.beginning_of_quarter # => Wed, 01 Jul 2015
today = Date.today # => Fri, 10 Jul 2015
Returns a new date/time at the start of the quarter.
def beginning_of_quarter first_quarter_month = month - (2 + month) % 3 beginning_of_month.change(month: first_quarter_month) end
def beginning_of_week(start_day = Date.beginning_of_week)
+Date.beginning_of_week+ or +config.beginning_of_week+ when set.
Week is assumed to start on +start_day+, default is
Returns a new date/time representing the start of this week on the given day.
def beginning_of_week(start_day = Date.beginning_of_week) result = days_ago(days_to_week_start(start_day)) acts_like?(:time) ? result.midnight : result end
def beginning_of_year
now = DateTime.current # => Fri, 10 Jul 2015 18:41:29 +0000
+DateTime+ objects will have a time set to 0:00.
today.beginning_of_year # => Thu, 01 Jan 2015
today = Date.today # => Fri, 10 Jul 2015
Returns a new date/time at the beginning of the year.
def beginning_of_year change(month: 1).beginning_of_month end
def copy_time_to(other)
def copy_time_to(other) other.change(hour: hour, min: min, sec: sec, nsec: try(:nsec)) end
def days_ago(days)
def days_ago(days) advance(days: -days) end
def days_since(days)
def days_since(days) advance(days: days) end
def days_span(day)
def days_span(day) (DAYS_INTO_WEEK.fetch(day) - DAYS_INTO_WEEK.fetch(Date.beginning_of_week)) % 7 end
def days_to_week_start(start_day = Date.beginning_of_week)
Week is assumed to start on +start_day+, default is
Returns the number of days to the start of the week on the given day.
def days_to_week_start(start_day = Date.beginning_of_week) start_day_number = DAYS_INTO_WEEK.fetch(start_day) (wday - start_day_number) % 7 end
def end_of_month
Returns a new date/time representing the end of the month.
def end_of_month last_day = ::Time.days_in_month(month, year) last_hour(days_since(last_day - day)) end
def end_of_quarter
now = DateTime.current # => Fri, 10 Jul 2015 18:41:29 +0000
+DateTime+ objects will have a time set to 23:59:59.
today.end_of_quarter # => Wed, 30 Sep 2015
today = Date.today # => Fri, 10 Jul 2015
Returns a new date/time at the end of the quarter.
def end_of_quarter last_quarter_month = month + (12 - month) % 3 beginning_of_month.change(month: last_quarter_month).end_of_month end
def end_of_week(start_day = Date.beginning_of_week)
+Date.beginning_of_week+ or +config.beginning_of_week+ when set.
Week is assumed to start on +start_day+, default is
Returns a new date/time representing the end of this week on the given day.
def end_of_week(start_day = Date.beginning_of_week) last_hour(days_since(6 - days_to_week_start(start_day))) end
def end_of_year
Returns a new date/time representing the end of the year.
def end_of_year change(month: 12).end_of_month end
def first_hour(date_or_time)
def first_hour(date_or_time) date_or_time.acts_like?(:time) ? date_or_time.beginning_of_day : date_or_time end
def future?
def future? self > self.class.current end
def last_hour(date_or_time)
def last_hour(date_or_time) date_or_time.acts_like?(:time) ? date_or_time.end_of_day : date_or_time end
def last_month
def last_month months_ago(1) end
def last_year
def last_year years_ago(1) end
def monday
Returns Monday of this week assuming that week starts on Monday.
def monday beginning_of_week(:monday) end
def months_ago(months)
def months_ago(months) advance(months: -months) end
def months_since(months)
def months_since(months) advance(months: months) end
def next_occurring(day_of_week)
today.next_occurring(:monday) # => Mon, 18 Dec 2017
today = Date.today # => Thu, 14 Dec 2017
Returns a new date/time representing the next occurrence of the specified day of week.
def next_occurring(day_of_week) from_now = DAYS_INTO_WEEK.fetch(day_of_week) - wday from_now += 7 unless from_now > 0 advance(days: from_now) end
def next_quarter
def next_quarter months_since(3) end
def next_week(given_day_in_next_week = Date.beginning_of_week, same_time: false)
now = DateTime.current # => Thu, 07 May 2015 13:31:16 +0000
+DateTime+ objects have their time set to 0:00 unless +same_time+ is true.
today.next_week(:friday) # => Fri, 15 May 2015
today = Date.today # => Thu, 07 May 2015
when set.
which is determined by +Date.beginning_of_week+ or +config.beginning_of_week+
The +given_day_in_next_week+ defaults to the beginning of the week
today.next_week # => Mon, 11 May 2015
today = Date.today # => Thu, 07 May 2015
Returns a new date/time representing the given day in the next week.
def next_week(given_day_in_next_week = Date.beginning_of_week, same_time: false) result = first_hour(weeks_since(1).beginning_of_week.days_since(days_span(given_day_in_next_week))) same_time ? copy_time_to(result) : result end
def next_weekday
def next_weekday if next_day.on_weekend? next_week(:monday, same_time: true) else next_day end end
def on_weekday?
def on_weekday? !WEEKEND_DAYS.include?(wday) end
def on_weekend?
def on_weekend? WEEKEND_DAYS.include?(wday) end
def past?
def past? self < self.class.current end
def prev_occurring(day_of_week)
today.prev_occurring(:monday) # => Mon, 11 Dec 2017
today = Date.today # => Thu, 14 Dec 2017
Returns a new date/time representing the previous occurrence of the specified day of week.
def prev_occurring(day_of_week) ago = wday - DAYS_INTO_WEEK.fetch(day_of_week) ago += 7 unless ago > 0 advance(days: -ago) end
def prev_quarter
def prev_quarter months_ago(3) end
def prev_week(start_day = Date.beginning_of_week, same_time: false)
+Date.beginning_of_week+ or +config.beginning_of_week+ when set.
Week is assumed to start on +start_day+, default is
Returns a new date/time representing the given day in the previous week.
def prev_week(start_day = Date.beginning_of_week, same_time: false) result = first_hour(weeks_ago(1).beginning_of_week.days_since(days_span(start_day))) same_time ? copy_time_to(result) : result end
def prev_weekday
def prev_weekday if prev_day.on_weekend? copy_time_to(beginning_of_week(:friday)) else prev_day end end
def sunday
Returns Sunday of this week assuming that week starts on Monday.
def sunday end_of_week(:monday) end
def today?
def today? to_date == ::Date.current end
def tomorrow
def tomorrow advance(days: 1) end
def tomorrow?
def tomorrow? to_date == ::Date.current.tomorrow end
def weeks_ago(weeks)
def weeks_ago(weeks) advance(weeks: -weeks) end
def weeks_since(weeks)
def weeks_since(weeks) advance(weeks: weeks) end
def years_ago(years)
def years_ago(years) advance(years: -years) end
def years_since(years)
def years_since(years) advance(years: years) end
def yesterday
def yesterday advance(days: -1) end
def yesterday?
def yesterday? to_date == ::Date.current.yesterday end