跳至內容 跳至搜尋
方法
#
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") # => "4月 25日, 2007" }, rfc822: "%d %b %Y", rfc2822: "%d %b %Y", iso8601: lambda { |date| date.iso8601 } }
 

屬性

[讀寫] 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()

傳回一個新的 `日期` 物件,表示今天之後 1 天的日期(即明天的日期)。

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

yesterday()

傳回一個新的 `日期` 物件,表示 1 天前的日期(即昨天的日期)。

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

實體公開方法

<=>(other)

acts_like_date?()

鴨子類型作為類似日期的類別。請參閱 `物件#acts_like?`

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

advance(options)

提供精確的 `日期` 計算,適用於年、月和日。`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)

`日期` 轉換為 `時間`(如果需要,則轉換為 `日期時間`),時間部分設定為一天的開始 (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()

`日期` 轉換為 `時間`(如果需要,則轉換為 `日期時間`),時間部分設定為一天的開始 (0:00)

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

change(options)

傳回一個新的 `日期`,其中一個或多個元素已根據 `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)

允許將 `日期``時間` 進行比較,方法是將其轉換為 `日期時間` 並依賴其中的 <=>。

也稱為:<=>
# 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()

`日期` 轉換為 `時間`(如果需要,則轉換為 `日期時間`),時間部分設定為一天的結束 (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()

`日期` 轉換為 `時間` (如果需要,則轉換為 `日期時間`),時間部分設定為一天的正午 (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 方法,例如「週一,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(秒數)

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