Rails dates with JSON - Part 2
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开发者_StackOverflow社区 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.
精彩评论