3

以下是一些输出:

Date.today  => Mon, 25 Jun 2012 
Date.today.to_formatted_s(:long_ordinal)  => "June 25th, 2012" 
Date.today.strftime('%A %d, %B')  =>  "Monday 25, June" 

现在我需要以下格式的输出,即:

  Monday 25th, June      or       Thrusday, 1st, October

问题是to_formatted_sstrftime仅适用于日期并且两者或它们都返回字符串。如何以我需要的方式获得输出?

4

1 回答 1

9

您可以使用Date::DATE_FORMATS添加新的自定义格式,并Integer.ordinalize获取日期序号:

Date::DATE_FORMATS[:month_ordinal] = lambda { |date| 
  date.strftime("%A #{date.day.ordinalize}, %B") 
}

>> Date.today.to_formatted_s(:month_ordinal)
=> "Monday 25th, June"
于 2012-06-25T06:39:02.657 回答