2011-01-30 81 views

回答

163

Date::MONTHNAMES[Date.today.month]會給你「一月」。 (您可能需要先登錄require 'date')。

+0

正是我在尋找的感謝 – 2011-01-30 17:45:29

+16

`Date.today.strftime(「%B」)`是這樣做的更好方法,恕我直言。它不是專用於導軌。請參閱下面的答案。 – 2014-09-01 20:14:12

+0

這不起作用了。它應該沒有標記爲正確的答案 – 2015-01-20 17:50:07

106

您也可以使用國際化:

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" 
11

對於Ruby 1.9的,我不得不使用:

Time.now.strftime("%B") 
10

如果你c是關於語言環境,你應該這樣做:

I18n.l(Time.now, format: "%B")