I can't seem to find this and I feel like it should be easy. In Ruby on Rails, how do I take:
2010-06-14 19:01:00 UTC
and turn it into
June 14th, 2010
Can I not just use a helper in the view?
I can't seem to find this and I feel like it should be easy. In Ruby on Rails, how do I take:
2010-06-14 19:01:00 UTC
and turn it into
June 14th, 2010
Can I not just use a helper in the view?
I don't know for
June 14th, 2010
But if you want
June 14, 2010
Ref how do i get name of the month in ruby on Rails? or this
Just do
@date = Time.now
@date.strftime("%B %d, %Y")
And for suffix use following
@date.strftime("%B #{@date.day.ordinalize}, %Y") # >>> Gives `June 18th, 2010`
For future reference: http://onrails.org/articles/2008/08/20/what-are-all-the-rails-date-formats
Just the other day there was a similar question. In my answer http://stackoverflow.com/questions/3022163/how-do-i-get-name-of-the-month-in-ruby-on-rails/3022466#3022466 I showed how you can add a custom to_s
definition in your config/environment.rb
file.
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
:my_own_long_date_format => "%B %d, %Y")
Now you can call Time.now.to_s(:my_own_long_date_format)
from any view to get:
June 15, 2010
Needs the Time module for Time.parse
and ActiveSupport for Integer#ordinalize
:
require 'time'
require 'active_support'
input = '2010-06-14 19:01:00 UTC'
t = Time.parse(input)
date = "%s %s, %d" % [t.strftime("%B"), t.day.ordinalize, t.year]
# => "June 14th, 2010"