这个月的名字(Date.today.month作为名字)
我使用Date.today.month
来显示月份编号。 是否有命令获取月份名称,还是我需要提出一个案例来获得它?
Date::MONTHNAMES[Date.today.month]
会给你“一月”。 (您可能需要先require 'date'
)。
你也可以使用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"
你可以使用strftime
:
Date.today.strftime("%B") # -> November
http://www.ruby-doc.org/stdlib-1.9.3/libdoc/date/rdoc/Date.html#strftime-method
对于Ruby 1.9,我必须使用:
Time.now.strftime("%B")
如果你关心语言环境,你应该这样做:
I18n.l(Time.now, format: "%B")