Skip to Content Skip to Search
Ruby on Rails 8.1.0

Module DateAndTime::Calculations

v8.1.0
Methods
A
B
D
E
F
L
M
N
O
P
Q
S
T
W
Y

Constants

DAYS_INTO_WEEK = { sunday: 0, monday: 1, tuesday: 2, wednesday: 3, thursday: 4, friday: 5, saturday: 6 }
WEEKEND_DAYS = [ 6, 0 ]

Instance Public methods

after?(date_or_time) Link

Returns true if the date/time falls after date_or_time.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 72
def after?(date_or_time)
 self > date_or_time
end

all_day() Link

Returns a Range representing the whole day of the current date/time.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 310
def all_day
 beginning_of_day..end_of_day
end

all_month() Link

Returns a Range representing the whole month of the current date/time.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 321
def all_month
 beginning_of_month..end_of_month
end

all_quarter() Link

Returns a Range representing the whole quarter of the current date/time.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 326
def all_quarter
 beginning_of_quarter..end_of_quarter
end

all_week(start_day = Date.beginning_of_week) Link

Returns a Range representing the whole week of the current date/time. Week starts on start_day, default is Date.beginning_of_week or config.beginning_of_week when set.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 316
def all_week(start_day = Date.beginning_of_week)
 beginning_of_week(start_day)..end_of_week(start_day)
end

all_year() Link

Returns a Range representing the whole year of the current date/time.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 331
def all_year
 beginning_of_year..end_of_year
end

at_beginning_of_month() Link

Alias for: beginning_of_month

at_beginning_of_quarter() Link

Alias for: beginning_of_quarter

at_beginning_of_week(start_day = Date.beginning_of_week) Link

Alias for: beginning_of_week

at_beginning_of_year() Link

Alias for: beginning_of_year

at_end_of_month() Link

Alias for: end_of_month

at_end_of_quarter() Link

Alias for: end_of_quarter

at_end_of_week(start_day = Date.beginning_of_week) Link

Alias for: end_of_week

at_end_of_year() Link

Alias for: end_of_year

before?(date_or_time) Link

Returns true if the date/time falls before date_or_time.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 67
def before?(date_or_time)
 self < date_or_time
end

beginning_of_month() Link

Returns a new date/time at the start of the month.

today = Date.today # => 2015年6月18日
today.beginning_of_month # => 2015年6月01日

DateTime objects will have a time set to 0:00.

now = DateTime.current # => 2015年6月18日 15:23:13 +0000
now.beginning_of_month # => 2015年6月01日 00:00:00 +0000
Also aliased as: at_beginning_of_month

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 125
def beginning_of_month
 first_hour(change(day: 1))
end

beginning_of_quarter() Link

Returns a new date/time at the start of the quarter.

today = Date.today # => 2015年7月10日
today.beginning_of_quarter # => 2015年7月01日

DateTime objects will have a time set to 0:00.

now = DateTime.current # => 2015年7月10日 18:41:29 +0000
now.beginning_of_quarter # => 2015年7月01日 00:00:00 +0000
Also aliased as: at_beginning_of_quarter

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 139
def beginning_of_quarter
 first_quarter_month = month - (2 + month) % 3
 beginning_of_month.change(month: first_quarter_month)
end

beginning_of_week(start_day = Date.beginning_of_week) Link

Returns a new date/time representing the start of this week on the given day. Week is assumed to start on start_day, default is Date.beginning_of_week or config.beginning_of_week when set. DateTime objects have their time set to 0:00.

Also aliased as: at_beginning_of_week

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 267
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

beginning_of_year() Link

Returns a new date/time at the beginning of the year.

today = Date.today # => 2015年7月10日
today.beginning_of_year # => 2015年1月01日

DateTime objects will have a time set to 0:00.

now = DateTime.current # => 2015年7月10日 18:41:29 +0000
now.beginning_of_year # => 2015年1月01日 00:00:00 +0000
Also aliased as: at_beginning_of_year

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 179
def beginning_of_year
 change(month: 1).beginning_of_month
end

days_ago(days) Link

Returns a new date/time the specified number of days ago.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 77
def days_ago(days)
 advance(days: -days)
end

days_since(days) Link

Returns a new date/time the specified number of days in the future.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 82
def days_since(days)
 advance(days: days)
end

days_to_week_start(start_day = Date.beginning_of_week) Link

Returns the number of days to the start of the week on the given day. Week is assumed to start on start_day, default is Date.beginning_of_week or config.beginning_of_week when set.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 258
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

end_of_month() Link

Returns a new date/time representing the end of the month. DateTime objects will have a time set to 23:59:59.

Also aliased as: at_end_of_month

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 296
def end_of_month
 last_day = ::Time.days_in_month(month, year)
 last_hour(days_since(last_day - day))
end

end_of_quarter() Link

Returns a new date/time at the end of the quarter.

today = Date.today # => 2015年7月10日
today.end_of_quarter # => 2015年9月30日

DateTime objects will have a time set to 23:59:59.

now = DateTime.current # => 2015年7月10日 18:41:29 +0000
now.end_of_quarter # => 2015年9月30日 23:59:59 +0000
Also aliased as: at_end_of_quarter

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 154
def end_of_quarter
 last_quarter_month = month + (12 - month) % 3
 beginning_of_month.change(month: last_quarter_month).end_of_month
end

end_of_week(start_day = Date.beginning_of_week) Link

Returns a new date/time representing the end of this week on the given day. Week is assumed to start on start_day, default is Date.beginning_of_week or config.beginning_of_week when set. DateTime objects have their time set to 23:59:59.

Also aliased as: at_end_of_week

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 283
def end_of_week(start_day = Date.beginning_of_week)
 last_hour(days_since(6 - days_to_week_start(start_day)))
end

end_of_year() Link

Returns a new date/time representing the end of the year. DateTime objects will have a time set to 23:59:59.

Also aliased as: at_end_of_year

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 304
def end_of_year
 change(month: 12).end_of_month
end

future?() Link

Returns true if the date/time is in the future.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 52
def future?
 self > self.class.current
end

last_month() Link

Short-hand for months_ago(1).

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 240
def last_month
 months_ago(1)
end

last_quarter() Link

Alias for: prev_quarter

last_week(start_day = Date.beginning_of_week, same_time: false) Link

Alias for: prev_week

last_weekday() Link

Alias for: prev_weekday

last_year() Link

Short-hand for years_ago(1).

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 251
def last_year
 years_ago(1)
end

monday() Link

Returns Monday of this week assuming that week starts on Monday. DateTime objects have their time set to 0:00.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 275
def monday
 beginning_of_week(:monday)
end

months_ago(months) Link

Returns a new date/time the specified number of months ago.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 97
def months_ago(months)
 advance(months: -months)
end

months_since(months) Link

Returns a new date/time the specified number of months in the future.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 102
def months_since(months)
 advance(months: months)
end

next_day?() Link

Alias for: tomorrow?

next_occurring(day_of_week) Link

Returns a new date/time representing the next occurrence of the specified day of week.

today = Date.today # => 2017年12月14日
today.next_occurring(:monday) # => 2017年12月18日
today.next_occurring(:thursday) # => 2017年12月21日

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 340
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

next_quarter() Link

Short-hand for months_since(3).

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 215
def next_quarter
 months_since(3)
end

next_week(given_day_in_next_week = Date.beginning_of_week, same_time: false) Link

Returns a new date/time representing the given day in the next week.

today = Date.today # => 2015年5月07日
today.next_week # => 2015年5月11日

The given_day_in_next_week defaults to the beginning of the week which is determined by Date.beginning_of_week or config.beginning_of_week when set.

today = Date.today # => 2015年5月07日
today.next_week(:friday) # => 2015年5月15日

DateTime objects have their time set to 0:00 unless same_time is true.

now = DateTime.current # => 2015年5月07日 13:31:16 +0000
now.next_week # => 2015年5月11日 00:00:00 +0000

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 200
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

next_weekday() Link

Returns a new date/time representing the next weekday.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 206
def next_weekday
 if next_day.on_weekend?
 next_week(:monday, same_time: true)
 else
 next_day
 end
end

on_weekday?() Link

Returns true if the date/time does not fall on a Saturday or Sunday.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 62
def on_weekday?
 !WEEKEND_DAYS.include?(wday)
end

on_weekend?() Link

Returns true if the date/time falls on a Saturday or Sunday.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 57
def on_weekend?
 WEEKEND_DAYS.include?(wday)
end

past?() Link

Returns true if the date/time is in the past.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 47
def past?
 self < self.class.current
end

prev_day?() Link

Alias for: yesterday?

prev_occurring(day_of_week) Link

Returns a new date/time representing the previous occurrence of the specified day of week.

today = Date.today # => 2017年12月14日
today.prev_occurring(:monday) # => 2017年12月11日
today.prev_occurring(:thursday) # => 2017年12月07日

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 351
def prev_occurring(day_of_week)
 ago = wday - DAYS_INTO_WEEK.fetch(day_of_week)
 ago += 7 unless ago > 0
 advance(days: -ago)
end

prev_quarter() Link

Short-hand for months_ago(3).

Also aliased as: last_quarter

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 245
def prev_quarter
 months_ago(3)
end

prev_week(start_day = Date.beginning_of_week, same_time: false) Link

Returns a new date/time representing the given day in the previous week. Week is assumed to start on start_day, default is Date.beginning_of_week or config.beginning_of_week when set. DateTime objects have their time set to 0:00 unless same_time is true.

Also aliased as: last_week

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 223
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

prev_weekday() Link

Returns a new date/time representing the previous weekday.

Also aliased as: last_weekday

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 230
def prev_weekday
 if prev_day.on_weekend?
 copy_time_to(beginning_of_week(:friday))
 else
 prev_day
 end
end

quarter() Link

Returns the quarter for a date/time.

Date.new(2010, 1, 31).quarter # => 1
Date.new(2010, 4, 12).quarter # => 2
Date.new(2010, 9, 15).quarter # => 3
Date.new(2010, 12, 25).quarter # => 4

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 166
def quarter
 (month / 3.0).ceil
end

sunday() Link

Returns Sunday of this week assuming that week starts on Monday. DateTime objects have their time set to 23:59:59.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 290
def sunday
 end_of_week(:monday)
end

today?() Link

Returns true if the date/time is today.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 30
def today?
 to_date == ::Date.current
end

tomorrow() Link

Returns a new date/time representing tomorrow.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 25
def tomorrow
 advance(days: 1)
end

tomorrow?() Link

Returns true if the date/time is tomorrow.

Also aliased as: next_day?

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 35
def tomorrow?
 to_date == ::Date.current.tomorrow
end

weeks_ago(weeks) Link

Returns a new date/time the specified number of weeks ago.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 87
def weeks_ago(weeks)
 advance(weeks: -weeks)
end

weeks_since(weeks) Link

Returns a new date/time the specified number of weeks in the future.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 92
def weeks_since(weeks)
 advance(weeks: weeks)
end

years_ago(years) Link

Returns a new date/time the specified number of years ago.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 107
def years_ago(years)
 advance(years: -years)
end

years_since(years) Link

Returns a new date/time the specified number of years in the future.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 112
def years_since(years)
 advance(years: years)
end

yesterday() Link

Returns a new date/time representing yesterday.

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 20
def yesterday
 advance(days: -1)
end

yesterday?() Link

Returns true if the date/time is yesterday.

Also aliased as: prev_day?

Source: | on GitHub

# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 41
def yesterday?
 to_date == ::Date.current.yesterday
end

AltStyle によって変換されたページ (->オリジナル) /