57

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!

7 Answers 7

119

Ref this

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

if

@date >> Fri, 11 Jun 2010

then

@date.strftime("%B") >> "June"
3
  • 3
    I wish there was Date.today.month_name.
    – Chloe
    Commented Jun 12, 2017 at 1:58
  • 2
    @Chloe: class Date; def month_name; self.strftime("%B"); end; end
    – MustModify
    Commented Feb 2, 2018 at 17:46
  • Date.today.strftime("%B")
    – gk12345
    Commented Jul 18, 2019 at 19:42
33

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"
   ]
1
  • 1
    Date::MONTHNAMES[Time.zone.today.month]
    – Mosaaleb
    Commented Aug 14, 2023 at 2:27
19

If you want a localized month name, try:

I18n.t('date.month_names')[date.month]

Example:

I18n.t('date.month_names')[12] #=> "Dezembro"
1
5

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

1
  • 2
    Look at date/time converters to_formatted_s. This allows you to define date formats (like :month_only) and use date.to_formatted_s(:month_only) so you get maximum reuse for your strftime usage. Commented Jun 11, 2010 at 15:03
4

Choose what you want

 Date.parse('5-jan-2017').strftime('%B')

Result:

=> "January"

Date.parse('5-jan-2017').strftime('%b')

Result:

=> "Jan"

For most of your further date related questions, refer this link http://www.foragoodstrftime.com/

0

this should help you https://www.shortcutfoo.com/app/dojos/ruby-dates/cheatsheet

Date::MONTHNAMES
Date::DAYNAMES
0

You can use locales to display month names:

# config/locales/en.yml
en:
  time:
    formats:
      default: "%F"
      short: "%b %d"
      long: "%b %d, %Y"
      month: "%b"

and to use it

i(Time.zone.today, format: :month)

date/time formats

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.