Module: DateAndTimeFormatting::InstanceMethods
- Defined in:
- lib/merb-helpers/date_time_formatting.rb
Instance Method Summary collapse
-
#formatted(format = :default) ⇒ Object
Formats a date/time instance using a defined format.
Instance Method Details
#formatted(format = :default) ⇒ Object
Formats a date/time instance using a defined format
Parameters
- format<Symbol>
-
of the format key from Date.date_formats
Returns
- String
-
formatted string
Example
Time.now.formatted(:rfc822) # => "Sun, 16 Nov 2007 00:21:16 -0800"
Time.now.formatted(:db) # => "2008-11-16 00:22:09"
You can also add your own formats using Date.add_format
when your app loads.
# Add the following to your init.rb
Merb::BootLoader.before_app_loads do
Date.add_format(:matt, "%H:%M:%S %Y-%m-%d")
end
# Format a Time instance with the format you just specified
Time.now.formatted(:matt) # => "00:00:00 2007-11-02"
–
38 39 40 |
# File 'lib/merb-helpers/date_time_formatting.rb', line 38 def formatted(format = :default) self.strftime(Date.formats[format]) end |