- #
- 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
。
来源:显示 | 在 GitHub 上
# 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
来源:显示 | 在 GitHub 上
# 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.zone
或 config.time_zone
,则返回 Time.zone
.today,否则仅返回 Date.today。
来源:显示 | 在 GitHub 上
# 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
。
来源:显示 | 在 GitHub 上
# 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 天(即明天的日期)。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 43 def tomorrow ::Date.current.tomorrow end
yesterday() 链接
返回一个新的 Date
,表示 1 天前(即昨天的日期)。
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 38 def yesterday ::Date.current.yesterday end
实例公共方法
acts_like_date?() 链接
作为 Date 类似类进行鸭子类型化。请参见 Object#acts_like?
。
来源:显示 | 在 GitHub 上
# 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
来源:显示 | 在 GitHub 上
# 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) 链接
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 55 def ago(seconds) in_time_zone.since(-seconds) end
beginning_of_day() 链接
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)
end_of_day() 链接
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 85 def end_of_day in_time_zone.end_of_day end
middle_of_day() 链接
来源:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 75 def middle_of_day in_time_zone.middle_of_day end
readable_inspect() 链接
用人类可读的方式覆盖默认的 inspect 方法,例如,“2005 年 2 月 21 日,星期一”
源代码:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date/conversions.rb, line 63 def readable_inspect strftime("%a, %d %b %Y") end
since(seconds) 链接
源代码:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 61 def since(seconds) in_time_zone.since(seconds) end
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}") }
源代码:显示 | 在 GitHub 上
# 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
。
源代码:显示 | 在 GitHub 上
# 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"
源代码:显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date/conversions.rb, line 95 def xmlschema in_time_zone.xmlschema end