Movatterモバイル変換


[0]ホーム

URL:


Skip to ContentSkip to Search
Ruby on Rails 8.1.1

Class Date<Object

v8.1.1
Methods
#
A
B
C
D
E
F
I
M
N
R
S
T
X
Y
Included Modules

Constants

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

Class Public methods

beginning_of_week()Link

Returns the week start (e.g.:monday) for the current request, if this has been set (viaDate.beginning_of_week=). IfDate.beginning_of_week has not been set for the current request, returns the week start specified inconfig.beginning_of_week. If noconfig.beginning_of_week was specified, returns:monday.

Source:show |on GitHub

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

beginning_of_week=(week_start)Link

SetsDate.beginning_of_week to a week start (e.g.:monday) for current request/thread.

This method accepts any of the following day symbols::monday,:tuesday,:wednesday,:thursday,:friday,:saturday,:sunday

Source:show |on GitHub

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

current()Link

ReturnsTime.zone.today whenTime.zone orconfig.time_zone are set, otherwise just returns Date.today.

Source:show |on GitHub

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

find_beginning_of_week!(week_start)Link

Returns week start day symbol (e.g.:monday), or raises anArgumentError for invalid day symbol.

Source:show |on GitHub

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 32deffind_beginning_of_week!(week_start)raiseArgumentError,"Invalid beginning of week: #{week_start}"unless::Date::DAYS_INTO_WEEK.key?(week_start)week_startend

tomorrow()Link

Returns a newDate representing the date 1 day after today (i.e. tomorrow’s date).

Source:show |on GitHub

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 43deftomorrow::Date.current.tomorrowend

yesterday()Link

Returns a newDate representing the date 1 day ago (i.e. yesterday’s date).

Source:show |on GitHub

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 38defyesterday::Date.current.yesterdayend

Instance Public methods

<=>(other)Link

Also aliased as:compare_without_coercion
Alias for:compare_with_coercion

acts_like_date?()Link

Duck-types as a Date-like class. SeeObject#acts_like?.

Source:show |on GitHub

# File activesupport/lib/active_support/core_ext/date/acts_like.rb, line 7defacts_like_date?trueend

advance(options)Link

Provides preciseDate calculations for years, months, and days. Theoptions parameter takes a hash with any of these keys::years,:months,:weeks,:days.

The increments are applied in order of time units from largest to smallest. In other words, the date is incremented first by:years, then by:months, then by:weeks, then by:days. This order can affect the result around the end of a month. For example, incrementing first by months then by days:

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

Whereas incrementing first by days then by months yields a different result:

Date.new(2004,9,30).advance(days:1).advance(months:1)# => Mon, 01 Nov 2004

Source:show |on GitHub

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 127defadvance(options)d =selfd =d>>options[:years]*12ifoptions[:years]d =d>>options[:months]ifoptions[:months]d =d+options[:weeks]*7ifoptions[:weeks]d =d+options[:days]ifoptions[:days]dend

ago(seconds)Link

ConvertsDate to aTime (orDateTime if necessary) with the time portion set to the beginning of the day (0:00) and then subtracts the specified number of seconds.

Source:show |on GitHub

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

at_beginning_of_day()Link

Alias for:beginning_of_day

at_end_of_day()Link

Alias for:end_of_day

at_midday()Link

Alias for:middle_of_day

at_middle_of_day()Link

Alias for:middle_of_day

at_midnight()Link

Alias for:beginning_of_day

at_noon()Link

Alias for:middle_of_day

beginning_of_day()Link

ConvertsDate to aTime (orDateTime if necessary) with the time portion set to the beginning of the day (0:00)

Also aliased as:midnight,at_midnight,at_beginning_of_day

Source:show |on GitHub

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 67defbeginning_of_dayin_time_zoneend

change(options)Link

Returns a newDate where one or more of the elements have been changed according to theoptions parameter. Theoptions parameter is a hash with a combination of these keys::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)

Source:show |on GitHub

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 143defchange(options)::Date.new(options.fetch(:year,year),options.fetch(:month,month),options.fetch(:day,day)  )end

compare_with_coercion(other)Link

AllowDate to be compared withTime by converting toDateTime and relying on the <=> from there.

Also aliased as:<=>

Source:show |on GitHub

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 152defcompare_with_coercion(other)ifother.is_a?(Time)to_datetime<=>otherelsecompare_without_coercion(other)endend

compare_without_coercion(other)Link

Alias for:<=>

default_inspect()Link

Alias for:inspect

end_of_day()Link

ConvertsDate to aTime (orDateTime if necessary) with the time portion set to the end of the day (23:59:59)

Also aliased as:at_end_of_day

Source:show |on GitHub

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 85defend_of_dayin_time_zone.end_of_dayend

in(seconds)Link

Alias for:since

inspect()Link

Also aliased as:default_inspect
Alias for:readable_inspect

midday()Link

Alias for:middle_of_day

middle_of_day()Link

ConvertsDate to aTime (orDateTime if necessary) with the time portion set to the middle of the day (12:00)

Also aliased as:midday,noon,at_midday,at_noon,at_middle_of_day

Source:show |on GitHub

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 75defmiddle_of_dayin_time_zone.middle_of_dayend

midnight()Link

Alias for:beginning_of_day

noon()Link

Alias for:middle_of_day

readable_inspect()Link

Overrides the default inspect method with a human readable one, e.g., “Mon, 21 Feb 2005”

Also aliased as:inspect

Source:show |on GitHub

# File activesupport/lib/active_support/core_ext/date/conversions.rb, line 63defreadable_inspectstrftime("%a, %d %b %Y")end

since(seconds)Link

ConvertsDate to aTime (orDateTime if necessary) with the time portion set to the beginning of the day (0:00) and then adds the specified number of seconds

Also aliased as:in

Source:show |on GitHub

# File activesupport/lib/active_support/core_ext/date/calculations.rb, line 61defsince(seconds)in_time_zone.since(seconds)end

to_formatted_s(format = :default)Link

Alias for:to_fs

to_fs(format = :default)Link

Convert to a formatted string. SeeDATE_FORMATS for predefined formats.

This method is aliased toto_formatted_s.

date =Date.new(2007,11,10)# => Sat, 10 Nov 2007date.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"

Adding your own date formats toto_fs

You can add your own formats to theDate::DATE_FORMATS hash. Use the format name as the hash key and either a strftime string or Proc instance that takes a date argument as the value.

# config/initializers/date_formats.rbDate::DATE_FORMATS[:month_and_year] ='%B %Y'Date::DATE_FORMATS[:short_ordinal] =->(date) {date.strftime("%B #{date.day.ordinalize}") }
Also aliased as:to_formatted_s

Source:show |on GitHub

# File activesupport/lib/active_support/core_ext/date/conversions.rb, line 49defto_fs(format =:default)ifformatter =DATE_FORMATS[format]ifformatter.respond_to?(:call)formatter.call(self).to_selsestrftime(formatter)endelseto_sendend

to_time(form = :local)Link

Converts aDate instance to aTime, where the time is set to the beginning of the day. The timezone can be either:local or:utc (default:local).

date =Date.new(2007,11,10)# => Sat, 10 Nov 2007date.to_time# => 2007-11-10 00:00:00 0800date.to_time(:local)# => 2007-11-10 00:00:00 0800date.to_time(:utc)# => 2007-11-10 00:00:00 UTC

NOTE: The:local timezone is Ruby’sprocess timezone, i.e.ENV['TZ']. If theapplication’s timezone is needed, then usein_time_zone instead.

Source:show |on GitHub

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

xmlschema()Link

Returns a string which represents the time in used time zone asDateTime defined by XML Schema:

date =Date.new(2015,05,23)# => Sat, 23 May 2015date.xmlschema# => "2015-05-23T00:00:00+04:00"

Source:show |on GitHub

# File activesupport/lib/active_support/core_ext/date/conversions.rb, line 95defxmlschemain_time_zone.xmlschemaend

[8]ページ先頭

©2009-2025 Movatter.jp