Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Custom date time format in rails 4

I want to display a custom date format in rails 4 by calling something like:

@reveiw.created_at.to_s(:submitted)

So far I have tried:

Time::DATE_FORMATS(:submitted) = "%b %d %Y"

in my config/environment.rb and config/initializers/time_formats.rb files.

I have also added the following code to my config/locales/en.yml file

en:
  time:
    formats:
      submitted: "%b %d %Y"

I have not had success. Is there a definitive strategy for rails 4?

like image 280
Michael van Holst Avatar asked Feb 07 '14 21:02

Michael van Holst


1 Answers

If you want to use @review.created_at.to_s(:submitted), I think you need to use Time::DATE_FORMATS[:submitted] = "%b %d %Y" (use square brackets instead of parentheses)

I believe the localization file output format is usable through Rails' #l helper http://guides.rubyonrails.org/i18n.html#adding-date-time-formats. So <%= l review.created_at, format: :submitted %> should work in an erb view

like image 115
sguha Avatar answered Sep 28 '22 06:09

sguha