Rails遵循JSON - 第2部分 [英] Rails dates with JSON - Part 2

查看:108
本文介绍了Rails遵循JSON - 第2部分的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



与json有关的日期



我正在尝试为日期对象(而不是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:

rails dates with json

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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆