跳至内容 跳至搜索
方法
#
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", rfc2822: "%d %b %Y", iso8601: lambda { |date| date.iso8601 } }
 

属性

[RW] beginning_of_week_default

类公开方法

beginning_of_week()

返回当前请求的周开始日期 (例如,:monday),如果已设置 (通过 Date.beginning_of_week=)。 如果未为当前请求设置 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)

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

at_end_of_day()

别名: end_of_day

at_midday()

别名: middle_of_day

at_middle_of_day()

别名: middle_of_day

at_midnight()

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)

允许 DateTime 进行比较,方法是转换为 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

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

noon()

别名: middle_of_day

readable_inspect()

用人类可读的格式覆盖默认的 inspect 方法,例如,“Mon, 21 Feb 2005”。

也称为: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(:rfc2822)       # => "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 49
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