26

I am implementing a Facebook application and using AJAX/JSON.

However the JSON structures that are returned have this format 2010-05-30T06:14:00Z.

I'm calling Game.all.to_json in controller action.

How can I convert them to a normal date format?

Is it easier to do it from the server side or the client side using fbjs? There are a lot of bugs with fbjs.

So i would prefer using a solution from the Server side using (Active Records). Like converting the data before sending the JSON structures.

Tobias Liefke
  • 8,637
  • 2
  • 41
  • 58
fenec
  • 5,637
  • 10
  • 56
  • 82
  • That works: http://stackoverflow.com/questions/5370061/format-date-time-in-find-operation-in-rails-3/7479550#7479550 – hipertracker Sep 20 '11 at 02:59

7 Answers7

47

The way I added my own custom format to the json I was returning was to add a monkey patch to the ActiveSupport TimeWithZone class.

Add a file in the config/initializers folder with the following contents:

class ActiveSupport::TimeWithZone
    def as_json(options = {})
        strftime('%Y-%m-%d %H:%M:%S')
    end
end
Geekygecko
  • 3,942
  • 2
  • 25
  • 21
  • 1
    This works great for me, but now I'm trying to do a similar thing for a Date object (returned using date_select). I've tried replacing ActiveSupport::TimeWithZone with ActiveSupport::Date, but no joy. Any help would be appreciated. Thanks. – Snips Feb 02 '11 at 11:37
  • 3
    Might be helpful to add %z for timezone. – tbeseda Feb 18 '11 at 18:45
  • @Geekygecko I upvoted your answer. I used this approach and it works great for a datetime field associated with a Model, not for datetime field from other table in a AREL query with joins. – GeorgeW Oct 27 '12 at 02:17
  • 1
    Ok here's a question: doesn't this completely destroy the functionality of the original as_json funtion ? If not, why not ? See http://stackoverflow.com/questions/9280744/rails-to-json-uses-different-date-formats-that-to-s – Cosmin Atanasiu Jun 25 '13 at 03:40
  • With timezone, strftime('%Y-%m-%d %H:%M:%S %z') – cevaris Apr 01 '14 at 18:52
7

With timezone working in all major browsers and ie7+:

class ActiveSupport::TimeWithZone
  def as_json(options = {})
    strftime('%Y/%m/%d %H:%M:%S %z')
  end
end
DrewB
  • 3,231
  • 24
  • 22
5

Rails now has the .as_json method on the DateTime object. Life made easier.

Igbanam
  • 5,904
  • 5
  • 44
  • 68
2

If you want to do this with client-side javascript, you can parse individual dates with something like this:

prettyDate = function(dateString) {
  var day, formatted, jsDate, month;

  jsDate = new Date(dateString);
  day = jsDate.getMonth() + 1 < 10 ? "0" + (jsDate.getMonth() + 1) : "" + (jsDate.getMonth() + 1);
  month = jsDate.getDate() < 10 ? "0" + (jsDate.getDate()) : "" + (jsDate.getDate());

  formatted = "" + day + "/" + month + "/" + (jsDate.getFullYear());
  return formatted;
};

You'll need some additional code if your needing to format time of course.

pizza247
  • 3,869
  • 7
  • 33
  • 47
2

You can override method as_json in your model as:

class Game
  def as_json(options = {})
    super.merge(time: time.strftime('%d.%m.%Y %H:%M:%S'))
  end
end
crabvk
  • 130
  • 1
  • 7
1

Definitely easier to do it on the server side. You can do a gsub regex to put it into the format you want, or a time.strftime, then generating your json with that string.

porkeypop
  • 324
  • 2
  • 11
0
Time.parse < datetime >

simples.

Baz
  • 36,440
  • 11
  • 68
  • 94
Mike Campbell
  • 7,921
  • 2
  • 38
  • 51