Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Date Format In RUBY

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.

like image 393
Rubyist Avatar asked Mar 18 '26 18:03

Rubyist


1 Answers

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)

like image 59
Zachary Anker Avatar answered Mar 20 '26 07:03

Zachary Anker



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!