Results 1 to 7 of 7

Thread: Any easy way to parse the JSON Date format used by Microsoft Asp.net ajax ?

  1. #1

    Default Any easy way to parse the JSON Date format used by Microsoft Asp.net ajax ?

    Microsoft ASP.NET Ajax uses "\/Date(628318530718)\/" to present a json string. I am wondering if it's a easy way to extend the Date prototype to accept this format?

  2. #2
    Sencha User trbs's Avatar
    Join Date
    Mar 2007
    Posts
    310

    Default

    The '628318530718' are the number of milliseconds since January 1, 1970 in Universal Coordinated Time (UTC).

    Which some slicing and dicing you can get a javascript date back from this (weird?) annotation.

    Code:
    >>> new Date(parseInt("\/Date(628318530718)\/".slice(6,18)))
    Wed Nov 29 1989 05:55:30 GMT+0100 (CET)

  3. #3

    Default

    Thanks trbs. Acutally I knew the number's the ticks since 1/1/1970.
    What I mean is Ext can't consume this format directly and I don't want to add an event handler in my data store to convert the format. I want to know if I can just extend the Date prototype to accept this format. Now I am using regex at my server side to replace this format back to standard M/d/yyyy format. I don't like it.

  4. #4
    Sencha User
    Join Date
    Mar 2007
    Posts
    7,854

    Default

    Why don't you like it? It would seem that MS is doing something unintuitive. Why not put the hack to handle it close to the source, so that if they fix it, you'll be able to just rip a couple lines of C# code?

  5. #5
    Ext JS Premium Member
    Join Date
    Nov 2008
    Posts
    53

    Default

    Ever find an answer to this? Mystix has tried to lead me in the right direction, however, I am still confused.

    http://weblogs.asp.net/bleroy/archiv...-and-json.aspx
    https://extjs.com/forum/showthread.php?t=31599&page=2

    the reg expression most people use to convert the date just turns the date into /Date(12432343243)/ How does that help? I tried using the slice method like up top and that was the only thing that actually got me a date. It's the wrong date, but at least it's a date. Others, in IE - Nan in Firefox - Invalid Date.

  6. #6
    Ext JS Premium Member
    Join Date
    Nov 2008
    Posts
    53

    Default

    Nevermind - this seems to work:
    http://extjs.com/forum/showthread.php?t=31599&page=2

    Code:
    function convertDate(v, mix) {
            return new Date(parseFloat(v.slice(6, 19))).toLocaleString();
        }
        
        var contentStore = new Ext.data.WCFJsonStore({
            url: 'Services/ContentManagementService.svc/getContents',
            baseParams: { channelSlug: '' },
            fields: ['Title', { name: 'DateModified', convert: convertDate }, 'ModifiedBy', { name: 'DateCreated', convert: convertDate }, 'CreatedBy']
        });
        contentStore.load();
    Not sure if it's the right way, but it works.

  7. #7
    Ext User
    Join Date
    Nov 2008
    Location
    Oakland, CA
    Posts
    155

    Default

    Hello All,

    I was just having this problem until finding your thread here. Thanks for the good information. I do have one question. How do I change the formatting of the returned date? It seems that using the following field declaration for the store field does not change the format.

    Code:
    { name: 'DateModified', convert: convertDate, type: 'date', dateFormat: 'M/dd/yyyy' },
    UPDATE: Here is what I've done for formatting. There might be a better way but it works.
    Code:
    return new Date( parseFloat(v.slice(6, 19)) ).format('mm/dd/yyyy').toLocaleString();

Posting Permissions

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