How can I get the date in UK format: 11th January 2008

This sounds trivial but it’s annoying me.

The UK format for a full date would be Friday 11th January 2008 or 11th
January 2008.

However, date.to_s(:long) gives “January 11, 2008”

Is there a way to get different dates out from it?

Cheers
Theo

This sounds trivial but it’s annoying me.

The UK format for a full date would be Friday 11th January 2008 or 11th
January 2008.

However, date.to_s(:long) gives “January 11, 2008”

Is there a way to get different dates out from it?

See the strftime method. I believe there is a way to add formats into
Rails “to_s” extension as well, but you’d have to look into that. See
ActiveSupport::CoreExtensions::Time::Conversions for more info.

time.strftime( string ) => string

Formats time according to the directives in the given format string. Any
text not listed as a directive will be passed through to the output
string.

Format meaning:

%a - The abbreviated weekday name (Sun'') %A - The full weekday name (Sunday’’)
%b - The abbreviated month name (Jan'') %B - The full month name (January’’)
%c - The preferred local date and time representation
%d - Day of the month (01…31)
%H - Hour of the day, 24-hour clock (00…23)
%I - Hour of the day, 12-hour clock (01…12)
%j - Day of the year (001…366)
%m - Month of the year (01…12)
%M - Minute of the hour (00…59)
%p - Meridian indicator (AM'' orPM’’)
%S - Second of the minute (00…60)
%U - Week number of the current year,
starting with the first Sunday as the first
day of the first week (00…53)
%W - Week number of the current year,
starting with the first Monday as the first
day of the first week (00…53)
%w - Day of the week (Sunday is 0, 0…6)
%x - Preferred representation for the date alone, no time
%X - Preferred representation for the time alone, no date
%y - Year without a century (00…99)
%Y - Year with century
%Z - Time zone name
%% - Literal ``%’’ character

t = Time.now
t.strftime("Printed on %m/%d/%Y")   #=> "Printed on 04/09/2003"
t.strftime("at %I:%M%p")            #=> "at 08:56A

To get the 11th you’ll need to use Inflector.ordinalize.

t = Time.now
t.strftime("%A") + Inflector.ordinalize(t.strftime("%d")) + " " +
t.strftime("%B %Y")

On Jan 11, 2008 1:37 PM, Philip H. [email protected] wrote:

See the strftime method. I believe there is a way to add formats into
Format meaning:
%m - Month of the year (01…12)
%x - Preferred representation for the date alone, no time


Ryan B.
http://www.frozenplague.net
Feel free to add me to MSN and/or GTalk as this email.

Cheers to both of you!

I’ll give this a try :slight_smile:

Okay,

I looked about and I just need to put this sort of thing in the
environment.rb file:

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
:uk_long => lambda { |time| time.strftime("%A #{time.day.ordinalize}
%B %Y") }
)

The long_ordinal format already puts the ‘th’ or whatever after the day
so I just altered the code in the API for that version.

Cheers
Theo

Theo Graham-brown wrote:

Okay,

I looked about and I just need to put this sort of thing in the
environment.rb file:

ActiveSupport::CoreExtensions::Time::Conversions::DATE_FORMATS.merge!(
:uk_long => lambda { |time| time.strftime("%A #{time.day.ordinalize}
%B %Y") }
)

The long_ordinal format already puts the ‘th’ or whatever after the day
so I just altered the code in the API for that version.

Cheers
Theo

Actually that solution didn’t work, but Ryan’s did. I’ll have to
investigate further at some point.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs