Question

I know I can manually make the date formats however I want with strftime, but are there any easy ways to quickly make dates look nicer than the :short, :long, :long_ordinal formats? The :long_ordinal looks nice but then doesn't have AM/PM, uses military time: September 11th, 2013 14:00 instead of September 11th, 2013 2:00 PM.

Ideally there's a kind of :long_ordinal_prettier format that has the 2:00 PM mechanism here in America and pretty local dates elsewhere in the world.

Était-ce utile?

La solution

Create a file like config/initializers/date_formats.rb

Date::DATE_FORMATS.merge!(
  long_date: '%a, %B %e, %Y %l:%M %p',
  short: '%A, %B %e, %Y',
  hyphen: "%Y-%m-%d",
  long_ordinal_prettier: "....."
)

You can use them like this

some_model.created_at.to_formatted_s(:long_ordinal_prettier)
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top