Ruby on rails 更改json数据的日期格式
我有JSON格式的数据。 我想将日期格式化为(d/m/y),但我有这种格式的记录 >“事件组结束”:“2069-12-31T00:00:00Z” 请问怎么做Ruby on rails 更改json数据的日期格式,ruby-on-rails,json,Ruby On Rails,Json,我有JSON格式的数据。 我想将日期格式化为(d/m/y),但我有这种格式的记录 >“事件组结束”:“2069-12-31T00:00:00Z” 请问怎么做 Data= [ { "country_group":null, "country_id":null, "created_at":"2013-07-02T14:43:28Z", "event_cou
Data= [
{
"country_group":null,
"country_id":null,
"created_at":"2013-07-02T14:43:28Z",
"event_country":"aut",
"event_group_city":"Innsbruck",
"event_group_end":"2069-12-31T00:00:00Z",
"event_group_start":"2069-12-31T00:00:00Z",
"event_group_title":"asdfasfsadf",
"event_location_id":null,
"id":11975,
"iso":null,
"status":0,
"updated_at":"2013-07-25T10:41:26Z",
"venue_id":"1027"
},
{
"country_group":null,
"country_id":null,
"created_at":"2013-07-02T14:43:26Z",
"event_country":"eng",
"event_group_city":"London",
"event_group_end":"2013-03-22T00:00:00Z",
"event_group_start":"2013-03-22T00:00:00Z",
"event_group_title":"jipj",
"event_location_id":null,
"id":11915,
"iso":null,
"status":0,
"updated_at":"2013-07-25T13:27:54Z",
"venue_id":"1264"
},
]
您可以在模型中编写访问器方法来获取格式化日期,并在定义的键中获取格式化日期
class EventModel < ActiveRecord::Base
#model code
def start_date_display
self[:event_group_start].strftime("%m/%d/%Y")
end
def end_date_display
self[:event_group_end].strftime("%m/%d/%Y")
end
end
您好,非常感谢您的回复。我已将模型
def start\u date\u display self[:event\u group\u start].strftime(“%m/%d/%Y”)end def更新为\u json super(:methods=>[:start\u date\u display])end
当我中止@data时。to\u json DATEFORMAT没有更改。
@data.to_json :methods => [:start_date_display,:end_date_display]
Data[1].each do |d|
d["event_group_end"].to_date.strftime("%d/%m/%Y")
end