2013-07-26 84 views
1

我有JSON數據形式。 我想格式化日期等(d/M/Y)但是我有記錄以這種格式 > 「event_group_end」: 「2069-12-31T00:00:00Z」更改json數據的日期格式

HOW TO DO這是嗎?

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" 
      }, 

      ] 

回答

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] 
+0

嗨@Mighty 感謝您的回覆。 我已經更新我的模型 'DEF start_date_display 自[:event_group_start] .strftime( 「%米/%d /%Y」) 端 DEF to_json 超級(:方法=> [:start_date_display]) 端' 當我做**放棄@ data.to_json ** DATEFORMAT沒有改變。 – Gagan

+0

請檢查更新的答案 –

0
Data[1].each do |d| 
    d["event_group_end"].to_date.strftime("%d/%m/%Y") 
end