Answers:
您也可以使用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
Time.new.strftime("B")
从Date中删除Today&strftime
如果您关心语言环境,则应执行以下操作:
I18n.l(Time.current, format: "%B")
I18n.l(Date.new, format: '%B')
从一月开始。如果您想增加月份,例如2月,请使用Date.new + 1.month