开发者

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

To format a Date in English I do:

Date.today.to_s(:long_ordinal)   # => September 28th, 2011

How could I format a d开发者_开发技巧ate in Russian (or any other language) ?


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 formats and locales are defined in ActiveSupport. You can add these values to your locale file and modify them to fit your requirements.
Also check this for russian language.


Use strftime. Example:

Date.today.strftime('%m/%d/%Y')

You set the %-symbols according to how you want the date to format. See the documentation I linked to see what symbols you can use.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜