如何处理`datetime`值以便将它从`2012-04-27 00:00:00 00改为2012年4月27日?

Bac*_*cko 2 ruby datetime ruby-on-rails date ruby-on-rails-3

我正在使用Ruby on Rails v3.2.2,我想显示一个类似的数据<day_number> <month_name> <year_number>.也就是说,我有一个datetime数据库表列,我想,以改变其包含的值,例如2012-04-27 00:00:0027 april 2012.

我该怎么做?

Sub*_*orx 5

标准方式是使用rails本地化.

模型

I18n.l(datetime, :format => :short)
Run Code Online (Sandbox Code Playgroud)

调节器

I18n.l(datetime, :format => :long)
Run Code Online (Sandbox Code Playgroud)

视图

<%= l(datetime), :format => :custom %>
Run Code Online (Sandbox Code Playgroud)

配置/语言环境/ en.yml

en:
  time:
    formats:
      long: "%A %B %d %Y | %I:%M %p GMT"
      short: "%d %B %Y, %H:%M GMT"
      custom: "your custom format"
  date:
    formats:
      short: "%d %B %Y"
      long: "%A %B %d %Y"
      custom: "your custom format"
Run Code Online (Sandbox Code Playgroud)

在您的情况下,格式应为"%d%A%Y".

好处是,如果您想要更改格式,您可以在一个地方为您使用的所有日期时间执行此操作.