Page 2 of 2 FirstFirst 12
Results 11 to 16 of 16

Thread: [2.0.1][Code/Patch] JSON Date Encoding / Decoding (UTC? Local?)

  1. #11

    Default DataContractJsonSerializer and dates

    I tried mystix's solution and it did indeed work like a charm. However instead of overwriting the entire Ext.data.Field class I used the following solution which seems cleaner to me.

    On the .NET side, instead of serializing a date datatype, why not pass a string as follows:
    Code:
    Dim eventDate as string = Date.Now.toString
    when passed by the datacontractjsonserializer the output would be this in the json:
    Code:
    "eventDate":"5\/1\/2009 11:23:09 AM"
    Then in your jsonStore define the field this way
    Code:
    var store = new Ext.data.JsonStore({
      fields: [{ name: 'eventDate', type: 'date', dateFormat: 'n/j/Y h:i:s A'}],
      listeners: {
        loadexception: function() {
          alert('-- load of data failed --');
        }
      }
    });
    This too works like a charm.

    Eric
    Last edited by mystix; 1 May 2009 at 7:53 AM. Reason: POST CODE IN [code][/code] TAGS. see http://extjs.com/forum/misc.php?do=bbcode#code

  2. #12

    Default Problem with dates before 1970-01-01

    The solution workes perfectly except when the dates are prehistoric, that is from before 1970-01-01. The regular expression does not take into account that there may be an - sign in front of the number. Change msdaterRe to
    var msdateRe = /\/Date\((-{0,1}\d+)\)\// // MS JSON date format
    And it will work for all dates.
    Regards MV

  3. #13
    Sencha User mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,232

    Default

    Quote Originally Posted by mvollset View Post
    The solution workes perfectly except when the dates are prehistoric, that is from before 1970-01-01. The regular expression does not take into account that there may be an - sign in front of the number. Change msdaterRe to
    var msdateRe = /\/Date\((-{0,1}\d+)\)\// // MS JSON date format
    And it will work for all dates.
    Regards MV
    are you sure m$ sends their hacky date strings with negative signs?
    do you have a link to the MRSF specs or something where i can verify this?

  4. #14

    Default MS Documentation

    See http://msdn.microsoft.com/en-us/library/bb412170.aspx this applies to VS2008 and it says the same in the VS2010 document, search for "DateTime wire format". Good thing you got me checking the documentation, I just discovered it because I had some "old dates".There will also be problems if you "jsoned" dates with timezone information, they are sent like "/Date(700000+0500)/". I am not excactly sure how this can be dealt with, i don't know the handling of dates in ext well enough. But a possible regexp would be:
    Date\((-{0,1}\d+)([+-]{0,1}\d{0,4})\), but from the MS documentation it appears like you can safely ignore the offset part. If that is the case the regexp above do not require any other codechanges.

    Regards MV

  5. #15
    Sencha User mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,232

    Default

    thanks for the link. i'll check it out later.

    in the meantime, you could try out this regex:
    Code:
    var msdateRe = /\/Date\(([-+])?(\d+)(?:[+-]\d{4})?\)\//;
    it correctly detects any leading positive/negative signs and ignores trailing timezone offsets.

    here's the corresponding calculation to boot:
    Code:
              var msdateMatch = v.match(msdateRe);
              if(msdateMatch) {
                  return new Date(((msdateMatch[1] || '') + msdateMatch[2]) * 1);
              }
    p.s. i've opted to ignore the timezone offset from some time back -- they are sending unix time after all, which is absolute time, so the offset isn't even required.
    Last edited by mystix; 19 Jun 2009 at 2:23 AM. Reason: added timezone detection

  6. #16
    Sencha User mystix's Avatar
    Join Date
    Mar 2007
    Location
    Singapore
    Posts
    6,232

    Default

    alternatively, in the next release of Ext 2.x / current release of 3.0rc2, you could avoid all this hackery altogether and simply specify your Record's Field definition like so:
    Code:
    {name: 'someDate', type: 'date', dateFormat: 'M$'}
    (note: i'll need to update the Date.parseFunction.M$ function with the above fix though, or you could do it yourself -- simply dig into the Date.js source for 3.0rc2)

Page 2 of 2 FirstFirst 12

Posting Permissions

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