Results 1 to 6 of 6

Thread: JSON encodeDate time zone

  1. #1
    Sencha Premium Member
    Join Date
    Oct 2011
    Location
    Duluth, MN
    Posts
    153
    Vote Rating
    23
      0  

    Default JSON encodeDate time zone

    Is there a reason that the timezone or at least the offset isn't included in the JSON encodeDate by default?

    Just curious if I'm missing something by overriding it since it's a global override.

    Thanks
    Bob

  2. #2
    Sencha - Sr Software Engineer mitchellsimoens's Avatar
    Join Date
    Mar 2007
    Location
    Gainesville, FL
    Posts
    39,556
    Vote Rating
    1272
      0  

    Default

    Code:
    Ext.JSON.encodeDate(new Date())
    returns

    Code:
    2012-05-28T09:03:30
    encodeDate method is:

    Code:
        this.encodeDate = function(o) {
            return '"' + o.getFullYear() + "-" 
            + pad(o.getMonth() + 1) + "-"
            + pad(o.getDate()) + "T"
            + pad(o.getHours()) + ":"
            + pad(o.getMinutes()) + ":"
            + pad(o.getSeconds()) + '"';
        };
    Mitchell Simoens @LikelyMitch
    Sencha Inc, Senior Software Engineer
    ________________
    Learn BBCode and use it! Checkout the CODE tag!

    Check out my GitHub, lots of nice things for Ext JS and Sencha Touch
    https://github.com/mitchellsimoens

  3. #3
    Sencha Premium Member
    Join Date
    Oct 2011
    Location
    Duluth, MN
    Posts
    153
    Vote Rating
    23
      0  

    Default

    Right, I was just wondering why the timezone isn't in the default implementation? Seems like if you are using JSON you are likely transmitting it to somewhere and that somewhere would want the timezone included. I could see it if it was UTC(GMT) time so you could assume that but it's not.

    I just didn't want to override encodeDate to include timezone and then wind up messing up a bunch of things since it's a global function.

    Thanks
    Bob

  4. #4
    Sencha User
    Join Date
    Nov 2010
    Location
    Ukraine
    Posts
    11
    Vote Rating
    0
      0  

    Default BUMP

    For my timezone (+0300):
    Code:
    JSON.stringify(new Date()) !== Ext.JSON.encodeDate(new Date())

    I found http://docs.sencha.com/extjs/4.2.0/s...hod-encodeDate

    Code:
    
    
        me.encodeDate = function(o) {
            return '"' + o.getFullYear() + "-"
            + pad(o.getMonth() + 1) + "-"
            + pad(o.getDate()) + "T"
            + pad(o.getHours()) + ":"
            + pad(o.getMinutes()) + ":"
            + pad(o.getSeconds()) + '"';
        };
    You should use o.getUTC*** methods instead

  5. #5
    Sencha Premium Member
    Join Date
    Jul 2014
    Posts
    20
    Vote Rating
    1
      0  

    Default Why hasn't this been fixed?

    I'm trying to figure out why this still hasn't been fixed!

  6. #6
    Sencha User
    Join Date
    Dec 2015
    Posts
    1
    Vote Rating
    0
      0  

    Default Not fixed but workaround

    I have also faced this problem. I encoded and decoded a datetime object and the results are two different dates since the timezone is left out of the decoded string.
    Lets say the result should be "2015-12-10T15:45:30-0300", but encoding resolves "2015-12-10T15:45:30".

    Clearly de sencha enconding left the timezone out of the string:
    this.encodeDate = function(o) {
    return '"' + o.getFullYear() + "-"
    + pad(o.getMonth() + 1) + "-"
    + pad(o.getDate()) + "T"
    + pad(o.getHours()) + ":"
    + pad(o.getMinutes()) + ":"
    + pad(o.getSeconds()) + '"';
    };

    The only workaround I found is that a datetime object should not be encoded decoded with its default value directly. It should be transformed to miliseconds with .getTime() method. That way the enconder leave the numeric value alone and the decoder parse it back to the original date value.

    Something like this:

    //enconding
    myDate = new Date();
    myDate = myDate.getTime();
    json = Ext.encode(myDate);

    //decoding
    decodedDate= Ext.decode(json );

    //myDate is equal to decodedDate

    I hope this bug gets fixed.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •