问题
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