Theo Graham-brown
2008-Jan-11 00:29 UTC
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg
2008-Jan-11 00:51 UTC
Re: How can I get the date in UK format: 11th January 2008
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 Hallstrom <rails-SUcgGwS4C16SUMMaM/qcSw@public.gmane.org> wrote:> > > 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'''' or ``PM'''') > %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 > > > >-- Ryan Bigg http://www.frozenplague.net Feel free to add me to MSN and/or GTalk as this email. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Philip Hallstrom
2008-Jan-11 03:07 UTC
Re: 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?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'''' or ``PM'''') %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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Theo Graham-brown
2008-Jan-11 07:56 UTC
Re: How can I get the date in UK format: 11th January 2008
Cheers to both of you! I''ll give this a try :-) -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Theo Graham-brown
2008-Jan-14 15:48 UTC
Re: How can I get the date in UK format: 11th January 2008
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Theo Graham-brown
2008-Jan-15 09:55 UTC
Re: How can I get the date in UK format: 11th January 2008
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 > TheoActually that solution didn''t work, but Ryan''s did. I''ll have to investigate further at some point. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---