Gagan
Gagan

Reputation: 573

change date format of json data

I have Data in form of JSON. I want to format date like (d/m/y) but i have record in this format > "event_group_end":"2069-12-31T00:00:00Z",

HOW TO DO THIS PLEASE?

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

            ]

Upvotes: 1

Views: 378

Answers (2)

Loken Makwana
Loken Makwana

Reputation: 3848

you can write accessor method in your model to get formatted date and get formatted date in defined keys

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

now when you call the json method do like this

@data.to_json :methods => [:start_date_display,:end_date_display]

Upvotes: 2

Siri Bunnamon
Siri Bunnamon

Reputation: 116

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

Upvotes: 0

Related Questions