how do I get name of the month in ruby on Rails?
Ref this
<% @date = Date.today %>
<%= @date.strftime("%B")%>
if
@date >> Fri, 11 Jun 2010
then
@date.strftime("%B") >> "June"
If you are looking solely to the month name, the Date::MONTHNAMES
constant provided by rails is the easiest solution for you:
Date::MONTHNAMES =
[
[ 0] nil,
[ 1] "January",
[ 2] "February",
[ 3] "March",
[ 4] "April",
[ 5] "May",
[ 6] "June",
[ 7] "July",
[ 8] "August",
[ 9] "September",
[10] "October",
[11] "November",
[12] "December"
]
If you want a localized month name, try:
I18n.t('date.month_names')[date.month]
Example:
I18n.t('date.month_names')[12] #=> "Dezembro"
If you have a particular custom date / time format which you need to use repeatedly then you can extend the ActiveSupport date / time helper.
e.g. if you define the following in your config/environment.rb
ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(:full_english => "%A %B %d, %Y at %I:%M %p")
then when you call Time.now.to_s(:full_english)
in your views you will get something like:
"Friday June 11, 2010 at 12:53 PM"
Ruby's strftime
method is well documented at http://apidock.com/ruby/Time/strftime