跳至内容 跳至搜索
方法
A
B
D
E
F
L
M
N
O
P
Q
S
T
W
Y

常量

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

实例公共方法

after?(date_or_time)

如果日期/时间在date_or_time之后,则返回true。

# 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()

返回一个范围,表示当前日期/时间的全天。

# 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()

返回一个范围,表示当前日期/时间的整个月份。

# 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()

返回一个范围,表示当前日期/时间的整个季度。

# 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)

返回一个范围,表示当前日期/时间的整个星期。星期从 start_day 开始,默认值为 Date.beginning_of_week,或当设置时为 config.beginning_of_week

# 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()

返回一个范围,表示当前日期/时间的整个年份。

# 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()

at_beginning_of_quarter()

at_beginning_of_week(start_day = Date.beginning_of_week)

at_beginning_of_year()

at_end_of_month()

别名:end_of_month

at_end_of_quarter()

at_end_of_week(start_day = Date.beginning_of_week)

别名:end_of_week

at_end_of_year()

别名:end_of_year

before?(date_or_time)

如果日期/时间在date_or_time之前,则返回true。

# 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()

返回一个新的日期/时间,表示月份的开始。

today = Date.today # => Thu, 18 Jun 2015
today.beginning_of_month # => Mon, 01 Jun 2015

DateTime 对象的时间将设置为 0:00。

now = DateTime.current # => Thu, 18 Jun 2015 15:23:13 +0000
now.beginning_of_month # => Mon, 01 Jun 2015 00:00:00 +0000
# 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()

返回一个新的日期/时间,表示季度的开始。

today = Date.today # => Fri, 10 Jul 2015
today.beginning_of_quarter # => Wed, 01 Jul 2015

DateTime 对象的时间将设置为 0:00。

now = DateTime.current # => Fri, 10 Jul 2015 18:41:29 +0000
now.beginning_of_quarter # => Wed, 01 Jul 2015 00:00:00 +0000
# 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)

返回一个新的日期/时间,表示这周的开始,在给定的日期。假设一周从 start_day 开始,默认值为 Date.beginning_of_week,或当设置时为 config.beginning_of_weekDateTime 对象的时间将设置为 0:00。

# 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()

返回一个新的日期/时间,表示今年的开始。

today = Date.today # => Fri, 10 Jul 2015
today.beginning_of_year # => Thu, 01 Jan 2015

DateTime 对象的时间将设置为 0:00。

now = DateTime.current # => Fri, 10 Jul 2015 18:41:29 +0000
now.beginning_of_year # => Thu, 01 Jan 2015 00:00:00 +0000
# 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)

返回一个新的日期/时间,表示指定天数之前的日期/时间。

# 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)

返回一个新的日期/时间,表示指定天数后的日期/时间。

# 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)

返回到本周给定日期的开始的天数。假设一周从 start_day 开始,默认值为 Date.beginning_of_week,或当设置时为 config.beginning_of_week

# 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()

返回一个新的日期/时间,表示该月的结束。DateTime 对象的时间将设置为 23:59:59。

也称为:at_end_of_month
# 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()

返回一个新的日期/时间,表示季度的结束。

today = Date.today # => Fri, 10 Jul 2015
today.end_of_quarter # => Wed, 30 Sep 2015

DateTime 对象的时间将设置为 23:59:59。

now = DateTime.current # => Fri, 10 Jul 2015 18:41:29 +0000
now.end_of_quarter # => Wed, 30 Sep 2015 23:59:59 +0000
也称为:at_end_of_quarter
# 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)

返回一个新的日期/时间,表示这周的结束,在给定的日期。假设一周从 start_day 开始,默认值为 Date.beginning_of_week,或当设置时为 config.beginning_of_weekDateTime 对象的时间将设置为 23:59:59。

也称为:at_end_of_week
# 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()

返回一个新的日期/时间,表示今年的结束。DateTime 对象的时间将设置为 23:59:59。

也称为:at_end_of_year
# 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?()

如果日期/时间在未来,则返回 true。

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

last_month()

months_ago(1) 的简写。

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

last_quarter()

别名:prev_quarter

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

别名:prev_week

last_weekday()

别名:prev_weekday

last_year()

years_ago(1) 的简写。

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

monday()

返回本周的星期一,假设一周从星期一开始。DateTime 对象的时间设置为 0:00。

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

months_ago(months)

返回一个新的日期/时间,表示指定个月数之前的时间。

# 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)

返回一个新的日期/时间,表示指定个月数之后的时间。

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

next_day?()

别名:tomorrow?

next_occurring(day_of_week)

返回一个新的日期/时间,表示指定的星期几的下次出现。

today = Date.today               # => Thu, 14 Dec 2017
today.next_occurring(:monday)    # => Mon, 18 Dec 2017
today.next_occurring(:thursday)  # => Thu, 21 Dec 2017
# 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()

months_since(3) 的简写。

# 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)

返回一个新的日期/时间,表示下周的给定日期。

today = Date.today # => Thu, 07 May 2015
today.next_week    # => Mon, 11 May 2015

given_day_in_next_week 默认情况下为一周的开始,该日期由 Date.beginning_of_weekconfig.beginning_of_week(如果设置)确定。

today = Date.today       # => Thu, 07 May 2015
today.next_week(:friday) # => Fri, 15 May 2015

除非 same_time 为 true,否则 DateTime 对象的时间将设置为 0:00。

now = DateTime.current # => Thu, 07 May 2015 13:31:16 +0000
now.next_week      # => Mon, 11 May 2015 00:00:00 +0000
# 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()

返回一个新的日期/时间,表示下一个工作日。

# 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?()

如果日期/时间不在星期六或星期日,则返回 true。

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

on_weekend?()

如果日期/时间在星期六或星期日,则返回 true。

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

past?()

如果日期/时间在过去,则返回 true。

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

prev_day?()

别名:yesterday?

prev_occurring(day_of_week)

返回一个新的日期/时间,表示指定的星期几的上次出现。

today = Date.today               # => Thu, 14 Dec 2017
today.prev_occurring(:monday)    # => Mon, 11 Dec 2017
today.prev_occurring(:thursday)  # => Thu, 07 Dec 2017
# 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()

months_ago(3) 的简写。

也称为:last_quarter
# 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)

返回一个新的日期/时间,表示上周的给定日期。假设一周从 start_day 开始,默认值为 Date.beginning_of_weekconfig.beginning_of_week(如果设置)。DateTime 对象的时间将设置为 0:00,除非 same_time 为 true。

也称为:last_week
# 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()

返回一个新的日期/时间,表示上一个工作日。

也称为:last_weekday
# 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()

返回日期/时间的季度。

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
# File activesupport/lib/active_support/core_ext/date_and_time/calculations.rb, line 166
def quarter
  (month / 3.0).ceil
end

sunday()

返回本周的星期日,假设一周从星期一开始。DateTime 对象的时间设置为 23:59:59。

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

today?()

如果日期/时间是今天,则返回 true。

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

tomorrow()

返回一个新的日期/时间,表示明天。

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

tomorrow?()

如果日期/时间是明天,则返回 true。

也称为:next_day?
# 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)

返回一个新的日期/时间,表示指定周数之前的时间。

# 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)

返回一个新的日期/时间,表示指定周数之后的时间。

# 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)

返回一个新的日期/时间,表示指定年数之前的时间。

# 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)

返回一个新的日期/时间,该日期/时间在未来指定年数。

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

yesterday()

返回一个表示昨天的新的日期/时间。

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

yesterday?()

如果日期/时间是昨天,则返回 true。

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