Need small help in converting date format in ruby

橙三吉。 提交于 2020-02-02 06:22:31

问题


Here are some outputs:

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" 

Now I need output in the format ie:

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

Problem is to_formatted_s and strftime apply only on date and both or them return string. How can I get the output in the way I need.


回答1:


You can use Date::DATE_FORMATS to add a new customized format, and Integer.ordinalize to get the day ordinal:

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

>> Date.today.to_formatted_s(:month_ordinal)
=> "Monday 25th, June"


来源:https://stackoverflow.com/questions/11184305/need-small-help-in-converting-date-format-in-ruby

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!