ExtJS JSON Date Serializer: TimeZones and Format

Through development, we finally ran into the Javascript date issue where the dates sent between the front and the backend were not in sync. The front end was sending the dates to the backend in “YYYY-MM_DD” format, and the Jackson’s deserializer would append the application’s server timezone to the date, and this would result in inconsistent dates in the right conditions since it assumed that the date sent was in GMT and would try to convert it to the local time which ended up shaving a day of the date. To solve this problem, we decided the frontend would send the date with the local timezone which would be saved on the server. It took me a little while to figure it out, but i finally used this piece of code to set the serializer’s date format:

Ext.util.JSON.encodeDate = function(o)
{
 return '"' + o.format("Y-m-d'T'H:i:s.uZ") + '"';
}

To set the correct format for your particular server, check the Date format patterns accepted by ExtJS Date object:

http://dev.sencha.com/deploy/dev/docs/?class=Ext.util.JSON&member=encodeDate

Advertisements

4 thoughts on “ExtJS JSON Date Serializer: TimeZones and Format

  1. Hi.

    Im using your piece of code and trying to send dates from extjs frontend to server code that uses resteasy with jackson to support the json serialization/deserialization. Im sending this date 2011-12-07’GMT’00:00:00.000-21600 and jackson is throwing this error:

    (error: Can not parse date “2011-12-07’GMT’00:00:00.000-21600”: not compatible with any of standard forms (“yyyy-MM-dd’T’HH:mm:ss.SSSZ”, “yyyy-MM-dd’T’HH:mm:ss.SSS’Z'”, “EEE, dd MMM yyyy HH:mm:ss zzz”, “yyyy-MM-dd”))

    What im doing wrong?

    Thanks

    1. I remember that error, I don’t have access to the code anymore but it definitely was a formatting error. It is a very small detail but check the formatting you are using against the formats returned in the error message.

      1. Thanks Abou,

        Finally i got it, im doing this as below:

        Ext.util.JSON.encodeDate = function(o)
        {
        return ‘”‘ + o.toISOString() + ‘”‘;
        }

        Now it works, regards from MX

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s