1

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

    2 Answers 2

    2

    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] 
    1
    • Hi @Mighty Thanks for reply. i have updated my model def start_date_display self[:event_group_start].strftime("%m/%d/%Y") end def to_json super(:methods=>[:start_date_display]) end when i do abort @data.to_json DATEFORMAT have not changed.
      – Gagan
      CommentedJul 26, 2013 at 12:30
    0
    Data[1].each do |d| d["event_group_end"].to_date.strftime("%d/%m/%Y") end 

      Start asking to get answers

      Find the answer to your question by asking.

      Ask question

      Explore related questions

      See similar questions with these tags.