Formatting a date object to display a human readable date
<%= time_ago_in_words @user.created_at %>
result: 9 hours ago
Date.to_s
is not the same as Time.to_s
. Your postdate
is a Date
, so therefore you might want to look at strftime
instead:
postdate.strftime("%B %d, %Y")
Or even look to add your own custom date format to your Rails app:
Need small help in converting date format in ruby
The to_formatted_s function already has some common human readable formats for DateTime
objects in Rails.
datetime.to_formatted_s(:db) # => "2007-12-04 00:00:00"
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"
datetime.to_formatted_s(:iso8601) # => "2007-12-04T00:00:00+00:00"