This month's name (Date.today.month as a name)

I am using Date.today.month to display the month number. Is there a team to get the name of the month, or do I need to make an occasion to get it?

+96
ruby
Jan 30 '11 at 17:15
source share
6 answers

Date::MONTHNAMES[Date.today.month] will give you "January". (You may need require 'date' ).

+176
Jan 30 '11 at 17:18
source share

You can also use I18n:

 I18n.t("date.month_names") # [nil, "January", "February", "March", "April", "May", "June", "July", "August", "September", "October", "November", "December"] I18n.t("date.abbr_month_names") # [nil, "Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"] I18n.t("date.month_names")[Date.today.month] # "December" I18n.t("date.abbr_month_names")[Date.today.month] # "Dec" 
+110
Dec 09 '11 at 16:21
source share

You can use strftime :

 Date.today.strftime("%B") # -> November 

http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#strftime-method

+72
Nov 25 '12 at 5:55
source share

If you care about the locale, you should do the following:

 I18n.l(Time.current, format: "%B") 
+13
Sep 03 '15 at 13:29
source share

For Ruby 1.9, I had to use:

 Time.now.strftime("%B") 
+12
May 08 '14 at 2:56
source share

HTML Choose month with I18n:

 <select> <option value="">Choose month</option> <%= 1.upto(12).each do |month| %> <option value="<%= month %>"><%= I18n.t("date.month_names")[month] %></option> <% end %> </select> 
0
Dec 06 '18 at 15:32
source share



All Articles