Date Format In RUBY

拈花ヽ惹草 提交于 2019-12-22 09:29:29

问题


Here, In RUBY I need a format of Like this

"February 1st 2011 11.00"

. I tried this one

"Time.now.utc.strftime("%B %d %Y %R")"

But I need to format numerals with suffixes st, nd, rd, th etc. Please suggest something.


回答1:


If you specifically want "February 1st 2011 11.00" you would need to add your own DATE_FORMATS and call that.

Time::DATE_FORMATS[:custom_ordinal] = lambda { |time| time.strftime("%B #{ActiveSupport::Inflector.ordinalize(time.day)} %Y %H.%M") }

puts Time.now.to_s(:custom_ordinal)
# February 28th 2011 02.12

If all you want is an ordinal date and "February 28th, 2011 02:12" works, then call Time.now.to_s(:long_ordinal)




回答2:


Install gem activesupport, and use method ordinalize:

How do I format a date in ruby to include "rd" as in "3rd"



来源:https://stackoverflow.com/questions/5140538/date-format-in-ruby

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