There are a couple of functions which you have to look up in any
language again and again, because they are slighty different in
each language, especially those for formatting of dates and
floating point numbers.

To display dates and datetimes in different formats in Ruby on
Rails applications, the following list for default formats
may help. The default date formats in Ruby on Rails are
:db, :time, :short, :long, :rfc822, see the Rails docs
for Time Conversions

t = Time.now
t.iso8601      # => "2008-09-24T18:52:18+02:00"
t.httpdate     # => "Wed, 24 Sep 2008 08:52:18 GMT"
t.rfc822       # => "Wed, 24 Sep 2008 18:52:18 +0200"
t.to_s         # => "Wed 24 Sep 18:52:18 +0200 2008"
t.to_s(:db)    # => "2008-09-24 18:52:18"
t.to_s(:time)  # => "18:52:18"
t.to_s(:short) # => "24 Sep 18:52"
t.to_s(:long)  # => "September 24, 2008 18:52"

They are defined as ..

:db     => "%Y-%m-%d %H:%M:%S",
:time   => "%H:%M",
:short  => "%d %b %H:%M",
:long   => "%B %d, %Y %H:%M",
:rfc822 => "%a, %d %b %Y %H:%M:%S %z"

..where the characters mean

%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 current year, starting with first Sunday as first day of first week
%W – Week number of current year, starting with first Monday as first day of first week
%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

And you can define also your own formats using
these formats. For example,

t = Time.now
t.strftime("Created on %m/%d/%Y") #=> "Created on 09/24/2008"
t.strftime("at %H:%M %p") #=> "at 10:52 AM"

A number of Ruby on Rails helpers exist to assist
in formatting floating point numbers, see here

The Ruby String class has a % method to format
strings, the usage is “str % value”: it uses
str as a format specification, and returns
the result of applying it to value

"%.3f" % (1.0/7)    # "0.143"
"%5.1f" % 345.6789  # "345.7"

"%05d" % 123      # "00123"
"%-5s" % "ID"     # "ID   "
"%04x" % 0xfc14   # "fc14"

"%6.2f, %05d" % [345.6789,123]  # "345.68, 00123"
"%-5s: %04x" % ["ID", 0xfc14]   # "ID   : fc14"

Update: this is one of the most popular posts of this blog! Since Rails 2.2, I18n (Internationalization) is available. With the new I18n API you can use I18n.localize() or I18n.l() to format dates. It is also possible to use custom formats and define them in your localization files.

config/de.yml:
  custom: "%d. %B um %H:%M Uhr"
I18n.localize(@time, :format => :short)
I18n.localize(@time, :format => :custom)
About these ads