method
to_formatted_s
v4.2.7 -
Show latest stable
-
0 notes -
Class: Time
- 1.0.0
- 1.1.6
- 1.2.6
- 2.0.3
- 2.1.0
- 2.2.1
- 2.3.8
- 3.0.0 (0)
- 3.0.9 (-1)
- 3.1.0 (0)
- 3.2.1 (0)
- 3.2.8 (0)
- 3.2.13 (0)
- 4.0.2 (0)
- 4.1.8 (2)
- 4.2.1 (0)
- 4.2.7 (0)
- 4.2.9 (0)
- 5.0.0.1 (0)
- 5.1.7 (0)
- 5.2.3 (0)
- 6.0.0 (0)
- 6.1.3.1 (0)
- 6.1.7.7 (0)
- 7.0.0 (0)
- 7.1.3.2 (-38)
- 7.1.3.4 (0)
- What's this?
to_formatted_s(format = :default)
public
Converts to a formatted string. See DATE_FORMATS for built-in formats.
This method is aliased to to_s.
time = Time.now # => Thu Jan 18 06:10:17 CST 2007 time.to_formatted_s(:time) # => "06:10" time.to_s(:time) # => "06:10" time.to_formatted_s(:db) # => "2007-01-18 06:10:17" time.to_formatted_s(:number) # => "20070118061017" time.to_formatted_s(:short) # => "18 Jan 06:10" time.to_formatted_s(:long) # => "January 18, 2007 06:10" time.to_formatted_s(:long_ordinal) # => "January 18th, 2007 06:10" time.to_formatted_s(:rfc822) # => "Thu, 18 Jan 2007 06:10:17 -0600" time.to_formatted_s(:iso8601) # => "2007-01-18T06:10:17-06:00"
Adding your own time formats to to_formatted_s
You can add your own formats to the Time::DATE_FORMATS hash. Use the format name as the hash key and either a strftime string or Proc instance that takes a time argument as the value.
# config/initializers/time_formats.rb Time::DATE_FORMATS[:month_and_year] = '%B %Y' Time::DATE_FORMATS[:short_ordinal] = ->(time) { time.strftime("%B #{time.day.ordinalize}") }