Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Rails jbuilder DateTime adding decimals to second

I'm using JBuilder to render the views of the JSON API part of my application. The problem I'm running into is that my DateTimes are being rendered like this:

"2013-07-02T17:03:18.000Z"

...when what I really want is this:

"2013-07-02T17:03:18Z"

I'm not sure where those decimals are coming from...

I'm rendering the field in the typical JBuilder way:

json.my_datetime_field

I have a date format initializer in my app:

Date::DATE_FORMATS[:default] = '%Y/%m/%d %Z'
Time::DATE_FORMATS[:default] = '%Y/%m/%d %H:%M:%S %Z'
Date::DATE_FORMATS[:month_day_year] = '%m-%d-%Y'

However, this doesn't seem to impact JBuilder, and that is good. I want ISO8601 format coming from my API. I'm on Rails 4.0.0 final, by the way.

like image 366
elsurudo Avatar asked Jul 03 '13 13:07

elsurudo


1 Answers

using under rails? to avoid encode with float, run this method on initialize block on your project

ActiveSupport::JSON::Encoding.time_precision = 0
like image 71
shao1555 Avatar answered Oct 11 '22 12:10

shao1555