You can define your own date formats.
The suggestions below did not work for me. I found another technique which updates ActiveSupport. Add the following to config/initializers/date_formats.rb:
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
:date => '%m/%d/%Y',
:date_time12 => "%m/%d/%Y %I:%M%p",
:date_time24 => "%m/%d/%Y %H:%M"
)
In your controller set the date that you want to display:
@cacheExpiresAt = 10.minutes.ago
In your ERB file, use the following to display the date value:
<%= @cacheExpiresAt.to_s(:date_time12) %>
The :date_time12 refers back to the entries you specified in date_formats.rb.
This technique allows you to have as many date formats as you’d like.
Rails ActiveSupport module adds a #to_formatted_s date method (from CoreExtensions::Time::Conversions and CoreExtensions::Date::Conversions). The extension has some default formats, such as :db, :short, and :long.
Create a file in config/initializers/. Call it date_formats.rb. Add your own formats to the initializer like this:
my_formats = {
:my_format_1 => '%l %p, %b %d, %Y',
:my_format_2 => '%l:%M %p, %B %d, %Y'
}
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(my_formats)
ActiveSupport::CoreExtensions::Date::Conversions::DATE_FORMATS.merge!(my_formats)
Then use it in your view like this:
<%= @post.created_at.to_formatted_s(:my_format_1) %>
Note that anything added in the initializers directory or in your environment.rb file require a restart in order to take effect.
There is a list of officially-supported strftime format strings in Ruby, but you can find a more complete, Ruby-compatible listing in the PHP documentation for strftime. Ruby’s strftime also supports undocumented modifiers to remove leading zeroes.
category: Howto
______________
Might be worth mentioning that changes to the environment will require a restart of the server.
Another easier way to do it is by using helper.
Edit helper/application_helper.rb
# Produces -> Thursday 25 May 2006 - 1:08 PM
def nice_date(date)
h date.strftime("%A %d %B %Y - %H:%M %p")
end
Then in your views, you can call that helper function by:
<%=nice_date event.end_time -%>
______________
Rails uses ParseDate.parsedate to create dates from form fields. See Parsing european date format in Ruby/Rails to extend input formats.