我有JSON格式的数据。 我想格式化日期,如(d / m / y),但我有这种格式的记录 的> “event_group_end”: “2069-12-31T00:00:00Z”,
如何做到这一点?
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"
},
]
答案 0 :(得分:2)
您可以在模型中编写访问器方法以获取格式化日期并在已定义的键中获取格式化日期
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
现在,当您调用json方法时,请执行此操作
@data.to_json :methods => [:start_date_display,:end_date_display]
答案 1 :(得分:0)
Data[1].each do |d|
d["event_group_end"].to_date.strftime("%d/%m/%Y")
end