Rails遵循JSON - 第2部分 [英] Rails dates with JSON - Part 2
问题描述
我正在尝试为日期对象(而不是datetime)做一个非常相似的事情,所以我修改了补丁如下:
类ActiveSupport :: Date#我已经将TimeWithZone更改为日期
def as_json(options = {})
strftime('%Y')
end
end
...但它不工作(日期格式与默认2011-02-03不变) p>
任何关于如何让这一点得到赞赏的提示。
Date对象不包含您希望格式化的小时,分钟和第二个属性。 DateTime和Time对象可以像您已经知道的那样执行。
I'm successfully using a duck punch to reformat the date returned by datetime, based on the helpful response given to someone else's question here:
I'm now trying to do a very similar thing for a date object (rather than datetime), so I've modified the patch as follows:
class ActiveSupport::Date # I've changed TimeWithZone to Date
def as_json(options = {})
strftime('%Y')
end
end
...but it's not working (the date format is unchanged from the default 2011-02-03).
Any hints on how to get this going much appreciated.
The Date object does not contain the hour, minute and second attributes you are wishing to format. DateTime and Time objects do, as you already know.
这篇关于Rails遵循JSON - 第2部分的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!