How to change the time format in rails 3.0.1

Can anybody help me?

In my opinion, I have: <% = post.time%>

It displays the line on the screen: Sat Jan 01 17:18:00 UTC 2000

Each line says "Sat Jan 01" + "UTC 2000"

How can I get rid of it and display only the time?

Thank you very much

+4
source share
2 answers
<%= post.date.strftime('%H:%M:%S') %> 

You can read the link to the full formatting syntax here . For instance:

% S - second of a minute (00..60)

% H - Hour of the day, 24-hour (00..23)

., etc.

+9
source

try something like this

  datetime = DateTime.civil(2007, 12, 4, 0, 0, 0, 0) # => Tue, 04 Dec 2007 00:00:00 +0000 datetime.to_formatted_s(:db) # => "2007-12-04 00:00:00" datetime.to_s(:db) # => "2007-12-04 00:00:00" datetime.to_s(:number) # => "20071204000000" datetime.to_formatted_s(:short) # => "04 Dec 00:00" datetime.to_formatted_s(:long) # => "December 04, 2007 00:00" datetime.to_formatted_s(:long_ordinal) # => "December 4th, 2007 00:00" datetime.to_formatted_s(:rfc822) # => "Tue, 04 Dec 2007 00:00:00 +0000" 

You can also add your own format:

  # config/initializers/time_formats.rb Time::DATE_FORMATS[:month_and_year] = "%B %Y" Time::DATE_FORMATS[:short_ordinal] = lambda { |time| time.strftime("%B #{time.day.ordinalize}") } 
+3
source

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


All Articles