daniel
daniel

Reputation: 3115

how to change time format in rails 3.0.1

anyone can help me with this?

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

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

In every row it says "Sat Jan 01" + "UTC 2000"

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

Many thanks

Upvotes: 4

Views: 7668

Answers (2)

Scott
Scott

Reputation: 17247

<%= post.date.strftime('%H:%M:%S') %>

You can read the reference for the full formatting syntax here. For example:

%S - Second of the minute (00..60)

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

. . . and so on.

Upvotes: 9

Andrei Andrushkevich
Andrei Andrushkevich

Reputation: 9973

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"

also you can add your custom format like this:

  # 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}") }

Upvotes: 3

Related Questions