- #
- 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 } } |
Attributes
| [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。
来源: 显示 | 在 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() 链接
来源: 显示 | 在 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,表示今天的后一天(即明天的日期)。
来源: 显示 | 在 GitHub 上
# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 43 def tomorrow ::Date.current.tomorrow end
yesterday() 链接
返回一个新的 Date,表示昨天的日期。
来源: 显示 | 在 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() 链接
来源: 显示 | 在 GitHub 上
# 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)
来源: 显示 | 在 GitHub 上
# 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) 链接
来源: 显示 | 在 GitHub 上
# 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
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 方法,使其更易读,例如,“Mon, 21 Feb 2005”
来源: 显示 | 在 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(: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}") }
来源: 显示 | 在 GitHub 上
# 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。
来源: 显示 | 在 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