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!
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.
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.
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 love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With