How to change the created_at format

It should be easy. Well, I'm sure it should be simple, these are rails.

The problem is this: in the model, all data has a created_at field. To get this information in the view, I use the block where the string t.created_at is located.

He shows me a result like 2015-04-12 11:04:44 UTC

What method should be used to display this date as 2015-04-12 ? I suppose it should be something like this: <i> t.created_at.date_only

could you help me?

+8
source share
3 answers

Read more about strftime here

 t.created_at.strftime("%Y-%m-%d") 
+25
source

And this can be made even simpler:

 t.created_at.to_date 
+10
source

Or with locales:

 // view: <% @articles.each do |c| %> <%= l c.created_at.to_date, format: :short %> <% end %> 
0
source

Source: https://habr.com/ru/post/985013/


All Articles