Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how do I get name of the month in ruby on Rails?

so i create in my view:

<%=date=Date.today%> 

How do i get the name of the month out of the date? I was trying to do sth like

 <%= DATE::ABBR_MONTHNAMES(date.month)%>     

But without success. I keep getting an error: uninitialized constant ActionView::Base::CompiledTemplates::MONTHNAMES

How do i initialise the constant or is there any other way to get the name out of the Date format?

would greatly appreciate any answers!

like image 501
necker Avatar asked Jun 11 '10 11:06

necker


People also ask

What is Strftime Ruby?

Ruby | Time strftime() function Time#strftime() is a Time class method which returns the time format according to the directives in the given format string. Syntax: Time.strftime() Parameter: Time values. Return: time format according to the directives in the given format string.

How do I change the Date format in Ruby on Rails?

You need to convert your string into Date object. For that, use Date#strptime . You can use Date#strftime to convert the Date object into preferred format.


2 Answers

Ref this

<% @date = Date.today  %>   <%= @date.strftime("%B")%> 

if

@date >> Fri, 11 Jun 2010 

then

@date.strftime("%B") >> "June" 
like image 149
Salil Avatar answered Oct 01 '22 05:10

Salil


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"    ] 
like image 39
Quentin Avatar answered Oct 01 '22 07:10

Quentin