class DateTime
def to_fs(format = :default)
Time::DATE_FORMATS[:month_and_year] = '%B %Y'
# config/initializers/time_formats.rb
datetime argument as the value.
either a strftime string or Proc instance that takes a time or
Time::DATE_FORMATS hash. Use the format name as the hash key and
DateTime formats are shared with Time. You can add your own to the
== Adding your own datetime formats to to_fs
datetime.to_fs(:iso8601) # => "2007-12-04T00:00:00+00:00"
datetime.to_fs(:rfc822) # => "Tue, 04 Dec 2007 00:00:00 +0000"
datetime.to_fs(:long_ordinal) # => "December 4th, 2007 00:00"
datetime.to_fs(:long) # => "December 04, 2007 00:00"
datetime.to_fs(:short) # => "04 Dec 00:00"
datetime.to_fs(:number) # => "20071204000000"
datetime.to_formatted_s(:db) # => "2007-12-04 00:00:00"
datetime.to_fs(:db) # => "2007-12-04 00:00:00"
datetime = DateTime.civil(2007, 12, 4, 0, 0, 0, 0) # => Tue, 04 Dec 2007 00:00:00 +0000
=== Examples
This method is aliased to to_formatted_s.
Convert to a formatted string. See Time::DATE_FORMATS for predefined formats.
def to_fs(format = :default) if formatter = ::Time::DATE_FORMATS[format] formatter.respond_to?(:call) ? formatter.call(self).to_s : strftime(formatter) else to_s end end