Custom Time Formats in Rails
If you need to set up custom date formats in Rails, for example to show in Views, you can do so by creating a config/initializers/time_formats.rb
file and adding as many of the following as you want:
Time::DATE_FORMATS[:au_datetime] = '%e/%m/%Y %I:%M%P'
Time::DATE_FORMATS[:au_date] = '%e/%m/%Y'
You can even use lambdas when defining a format, which will be executed using .call
when you call to_s
on your Time
:
Time::DATE_FORMATS[:short_ordinal] = ->(time) { time.strftime("%B #{time.day.ordinalize}") }
This is covered in more detail in the Rails Time
documentation here:
http://api.rubyonrails.org/classes/Time.html#method-i-to_formatted_s