1

I use jbuilder to output my data.

However, the date in JS will look like this 2016-09-01T02:39:08.495Z How could I make Rails output any datetime-type field with this format 2016-09-01 02:39, discard the timezone information in the JSON output.

It seems there's no elegant way to make this option be default.

That is I need to manually put the conversion into any field I want to convert for all jbuilder templates.

newBike
  • 14,385
  • 29
  • 109
  • 192
  • sounds pretty much the same as this question: http://stackoverflow.com/questions/2937740/rails-dates-with-json – neongrau Aug 23 '16 at 07:41

1 Answers1

0

You can always use formatting: Time.zone.now.strftime("%Y-%m-%d %H:%m")

If you're planning on using this in many places in your Rails app, you can add the foll. to your config/application.rb file:

class Time
  def without_timezone
    self.strftime("%Y-%m-%d %H:%m")
  end
end

Then you can call without_timezone method on the Time object anywhere in your Rails app, including jbuilder.

Time.zone.now
> Tue, 23 Aug 2016 19:59:17 UTC +00:00
Time.zone.now.without_timezone
> "2016-08-23 20:08"
tekina
  • 571
  • 10
  • 21