跳至内容 跳至搜索
方法
#
A
B
C
D
E
F
I
M
N
R
S
T
X
Y
包含模块

常量

DATE_FORMATS = { short: "%d %b", long: "%B %d, %Y", db: "%Y-%m-%d", inspect: "%Y-%m-%d", number: "%Y%m%d", long_ordinal: lambda { |date| day_format = ActiveSupport::Inflector.ordinalize(date.day) date.strftime("%B #{day_format}, %Y") # => "April 25th, 2007" }, rfc822: "%d %b %Y", iso8601: lambda { |date| date.iso8601 } }
 

属性

[RW] beginning_of_week_default

类公共方法

beginning_of_week()

如果已设置(通过 Date.beginning_of_week=),则返回当前请求的周开始(例如 :monday)。如果未为当前请求设置 Date.beginning_of_week,则返回 config.beginning_of_week 中指定的周开始。如果未指定 config.beginning_of_week,则返回 :monday

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 19
def beginning_of_week
  ::ActiveSupport::IsolatedExecutionState[:beginning_of_week] || beginning_of_week_default || :monday
end

beginning_of_week=(week_start)

Date.beginning_of_week 设置为当前请求/线程的周开始(例如 :monday)。

此方法接受以下任何一个日期符号::monday:tuesday:wednesday:thursday:friday:saturday:sunday

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 27
def beginning_of_week=(week_start)
  ::ActiveSupport::IsolatedExecutionState[:beginning_of_week] = find_beginning_of_week!(week_start)
end

current()

如果设置了 Time.zoneconfig.time_zone,则返回 Time.zone.today,否则仅返回 Date.today。

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 48
def current
  ::Time.zone ? ::Time.zone.today : ::Date.today
end

find_beginning_of_week!(week_start)

返回周开始日期符号(例如 :monday),或针对无效日期符号引发 ArgumentError

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 32
def find_beginning_of_week!(week_start)
  raise ArgumentError, "Invalid beginning of week: #{week_start}" unless ::Date::DAYS_INTO_WEEK.key?(week_start)
  week_start
end

tomorrow()

返回一个新的 Date,表示今天之后的第 1 天(即明天的日期)。

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 43
def tomorrow
  ::Date.current.tomorrow
end

yesterday()

返回一个新的 Date,表示 1 天前(即昨天的日期)。

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 38
def yesterday
  ::Date.current.yesterday
end

实例公共方法

<=>(other)

也别名为:compare_without_coercion

acts_like_date?()

作为 Date 类似类进行鸭子类型化。请参见 Object#acts_like?

# File activesupport/lib/active_support/core_ext/date/acts_like.rb, line 7
def acts_like_date?
  true
end

advance(options)

提供精确的 Date 计算,用于年、月和日。options 参数采用一个哈希表,其中包含以下任一键::years:months:weeks:days

增量按时间单位从大到小的顺序应用。换句话说,日期首先按 :years 增量,然后按 :months 增量,然后按 :weeks 增量,最后按 :days 增量。此顺序可能会影响月底附近的结果。例如,先按月增量,再按日增量

Date.new(2004, 9, 30).advance(months: 1, days: 1)
# => Sun, 31 Oct 2004

而先按日增量,再按月增量则会产生不同的结果

Date.new(2004, 9, 30).advance(days: 1).advance(months: 1)
# => Mon, 01 Nov 2004
# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 127
def advance(options)
  d = self

  d = d >> options[:years] * 12 if options[:years]
  d = d >> options[:months] if options[:months]
  d = d + options[:weeks] * 7 if options[:weeks]
  d = d + options[:days] if options[:days]

  d
end

ago(seconds)

Date 转换为 Time(或 DateTime,如果需要),并将时间部分设置为一天的开始时间(0:00),然后减去指定秒数。

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 55
def ago(seconds)
  in_time_zone.since(-seconds)
end

at_beginning_of_day()

别名:beginning_of_day

at_end_of_day()

别名:end_of_day

at_midday()

别名:middle_of_day

at_middle_of_day()

别名:middle_of_day

at_midnight()

别名:beginning_of_day

at_noon()

别名:middle_of_day

beginning_of_day()

Date 转换为 Time(或 DateTime,如果需要)并将时间部分设置为一天的开始(0:00)

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 67
def beginning_of_day
  in_time_zone
end

change(options)

返回一个新的 Date,其中一个或多个元素已根据 options 参数进行更改。options 参数是一个哈希,其中包含以下键的组合::year:month:day

Date.new(2007, 5, 12).change(day: 1)               # => Date.new(2007, 5, 1)
Date.new(2007, 5, 12).change(year: 2005, month: 1) # => Date.new(2005, 1, 12)
# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 143
def change(options)
  ::Date.new(
    options.fetch(:year, year),
    options.fetch(:month, month),
    options.fetch(:day, day)
  )
end

compare_with_coercion(other)

允许 Date 通过转换为 DateTimeTime 进行比较,并依赖于 DateTime 中的 <=>。

别名:<=>
# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 152
def compare_with_coercion(other)
  if other.is_a?(Time)
    to_datetime <=> other
  else
    compare_without_coercion(other)
  end
end

compare_without_coercion(other)

别名:<=>

default_inspect()

别名:inspect

end_of_day()

Date 转换为 Time(或 DateTime,如果需要),并将时间部分设置为一天的结束(23:59:59)

别名:at_end_of_day
# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 85
def end_of_day
  in_time_zone.end_of_day
end

in(seconds)

别名:since

inspect()

别名:default_inspect
别名:readable_inspect

midday()

别名:middle_of_day

middle_of_day()

Date 转换为 Time(或 DateTime,如果需要),并将时间部分设置为一天的中间(12:00)

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 75
def middle_of_day
  in_time_zone.middle_of_day
end

midnight()

别名:beginning_of_day

noon()

别名:middle_of_day

readable_inspect()

用人类可读的方式覆盖默认的 inspect 方法,例如,“2005 年 2 月 21 日,星期一”

别名:inspect
# File activesupport/lib/active_support/core_ext/date/conversions.rb, line 63
def readable_inspect
  strftime("%a, %d %b %Y")
end

since(seconds)

Date 转换为 Time(或 DateTime,如果需要),并将时间部分设置为一天的开始(0:00),然后添加指定数量的秒数

别名:in
# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 61
def since(seconds)
  in_time_zone.since(seconds)
end

to_formatted_s(format = :default)

别名:to_fs

to_fs(format = :default)

转换为格式化的字符串。有关预定义格式,请参见 DATE_FORMATS

此方法的别名为 to_formatted_s

date = Date.new(2007, 11, 10)       # => Sat, 10 Nov 2007

date.to_fs(:db)                     # => "2007-11-10"
date.to_formatted_s(:db)            # => "2007-11-10"

date.to_fs(:short)         # => "10 Nov"
date.to_fs(:number)        # => "20071110"
date.to_fs(:long)          # => "November 10, 2007"
date.to_fs(:long_ordinal)  # => "November 10th, 2007"
date.to_fs(:rfc822)        # => "10 Nov 2007"
date.to_fs(:iso8601)       # => "2007-11-10"

将自己的日期格式添加到 to_fs

可以将自己的格式添加到 Date::DATE_FORMATS 哈希表中。使用格式名称作为哈希键,并将 strftime 字符串或将日期参数作为参数的 Proc 实例作为值。

# config/initializers/date_formats.rb
Date::DATE_FORMATS[:month_and_year] = '%B %Y'
Date::DATE_FORMATS[:short_ordinal] = ->(date) { date.strftime("%B #{date.day.ordinalize}") }
别名:to_formatted_s
# File activesupport/lib/active_support/core_ext/date/conversions.rb, line 47
def to_fs(format = :default)
  if formatter = DATE_FORMATS[format]
    if formatter.respond_to?(:call)
      formatter.call(self).to_s
    else
      strftime(formatter)
    end
  else
    to_s
  end
end

to_time(form = :local)

Date 实例转换为 Time,其中时间设置为一天的开始。时区可以是 :local:utc(默认 :local)。

date = Date.new(2007, 11, 10)  # => Sat, 10 Nov 2007

date.to_time                   # => 2007-11-10 00:00:00 0800
date.to_time(:local)           # => 2007-11-10 00:00:00 0800

date.to_time(:utc)             # => 2007-11-10 00:00:00 UTC

注意::local 时区是 Ruby 的进程时区,即 ENV['TZ']。如果需要应用程序时区,请改用 in_time_zone

# File activesupport/lib/active_support/core_ext/date/conversions.rb, line 83
def to_time(form = :local)
  raise ArgumentError, "Expected :local or :utc, got #{form.inspect}." unless [:local, :utc].include?(form)
  ::Time.public_send(form, year, month, day)
end

xmlschema()

返回一个字符串,表示 XML Schema 定义的 DateTime 中使用的时区中的时间

date = Date.new(2015, 05, 23)  # => Sat, 23 May 2015
date.xmlschema                 # => "2015-05-23T00:00:00+04:00"
# File activesupport/lib/active_support/core_ext/date/conversions.rb, line 95
def xmlschema
  in_time_zone.xmlschema
end