0

I'm trying to format a date in Rails 3 using the new syntax as described in the code:

http://github.com/rails/rails/blob/master/activesupport/lib/active_support/core_ext/date/conversions.rb

by using the following syntax in an initialiser:

Date::DATE_FORMATS[:my_format] = '%m %d %Y'

I am then referencing dates in my view like so:

comment.created_at.to_formatted_s(:my_format)

However, I'm just getting the default formatting - what could be wrong?

4

2 回答 2

1

有两个类,时间和日期。

如果你有一个时间对象,你会使用:

时间::DATE_FORMATS[:my_format] = '%m %d %Y'

如果您有一个日期对象,您将使用:

日期::DATE_FORMATS[:my_format] = '%m %d %Y'

所以答案是将这两种样式都放在你的初始化文件中。

于 2010-11-19T22:15:57.697 回答
0

似乎需要以下“to_date”:

comment.created_at.to_date.to_formatted_s(:my_format)
于 2010-06-08T10:30:18.613 回答