How to Format a Date in Ruby to Include "Rd" as in "3Rd"

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

I'm going to echo everyone else, but I'll just encourage you to download the activesupport gem, so you can just use it as a library. You don't need all of Rails to use ordinalize.


% gem install activesupport
...
% irb
irb> require 'rubygems'
#=> true
irb> require 'activesupport'
#=> true
irb> 3.ordinalize
#=> "3rd"

In Ruby on Rails, how do I format a date with the th suffix, as in, Sun Oct 5th ?

Use the ordinalize method from 'active_support'.

>> time = Time.new
=> Fri Oct 03 01:24:48 +0100 2008
>> time.strftime("%a %b #{time.day.ordinalize}")
=> "Fri Oct 3rd"

Note, if you are using IRB with Ruby 2.0, you must first run:

require 'active_support/core_ext/integer/inflections'

Suffixes on dates (1st, 2nd, 3rd, 4th etc)

If you're able (and willing) to bring a Ruby gem into the mix, consider ActiveSupport::Inflector.
You can install it with


gem install active_support

(you might need sudo)

then require it in your file and include ActiveSupport::Inflector:

require 'active_support/inflector' # loads the gem
include ActiveSupport::Inflector # brings methods to current namespace

then you can ordinalize integers willy-nilly:

ordinalize(1)  # => "1st"
ordinalize(13) # => "13th"

You might have to stringify your date by hand, though:

date = iptc["DateTimeOriginal"]
date_string = date.strftime('%A ordinalday %B %Y')
date_string.sub!(/ordinalday/, ordinalize(date.day))
date_string.upcase!

and you should be on your way:

iptc["Caption"] = "#{date_string}: #{caption} #{location}"

Ruby DateTime format: How can I get 1st, 2nd, 3rd, 4th?

You might want to take a look here.

To summarize

time = DateTime.now
time.strftime("%A, %B #{time.day.ordinalize} %Y")

Note that you are running in plain Ruby (2.0) you'll need to call:

require 'active_support/core_ext/integer/inflections'

Rails 3: How to format a Date in other than English languages?

There is the Internationalization API in Rails that explains how to do that. See for example section 3.2 Adding Date/Time Formats.

<p><%= l Date.today, :format => :short %></p>

You can then add for your locale (russian?) a file ru.yml and include there the definition for your format.

# config/locales/ru.yml
ru:
date:
formats:
short: "%m/%d/%Y"

See the section 4.4 Setting and Passing a Locale how to set the locale.

Date Format In RUBY

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)

converting a date in ruby to a customised format

You can try this

t = Time.now()
t.strftime("#{t.day.ordinalize} %B %Y")

It will result in

27th November 2013

Ruby formatting for ordinals: '1' as '1st', '2' as '2nd' etc

Looks like you are looking for ordinalize:

The Ruby on Rails framework is chock full of interesting little nuggets. Ordinalize is a number extension that returns the corresponding ordinal number as a string. For instance, 1.ordinalize returns “1st” and 22.ordinalize return “22nd”.

Example:

place = 3
puts "You are currently in #{place.ordinalize} place."

Result:

You are currently in 3rd place.



Related Topics



Leave a reply



Submit