Date format in RUBY

Here in RUBY I need a Like this format

"February 1, 2011 11.00"

. I tried this one

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

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

+3
source share
2 answers

If you specifically want to "February 1, 2011 11.00", you will need to add your own DATE_FORMATS and call it.

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 need is an ordinal date and "February 28, 2011 02:12" works, then Time.now.to_s (: long_ordinal) is called

+5
source

Install gem activesupportand use the method ordinalize:

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

+1

Source: https://habr.com/ru/post/1795190/


All Articles