How to handle json DateTime returned from WCF Data Services (OData)

前端 未结 8 784
情书的邮戳
情书的邮戳 2020-12-01 11:19

I believe I am missing something obvious here. When I request a JSON response from an OData service I get a different result for the DateTime properties than I do when I req

相关标签:
8条回答
  • 2020-12-01 11:24

    This should work just fine:

    var date = new Date(parseInt(jsonDate.substr(6)));
    

    The substr function takes out the "/Date(" part, and the parseInt function gets the integer and ignores the ")/" at the end.

    For ISO-8601 formatted JSON dates, just pass the string into the Date constructor:

    var date = new Date(jsonDate); //no ugly parsing needed; full timezone support
    

    This was already fixed and discussed that a look at this previous post

    0 讨论(0)
  • 2020-12-01 11:24

    This reply might get voted down (!!) but an alternative solution is to just change your WCF Service to return the dates in a more friendly way.

    Here's some sample JSON from my WCF service, showing a UpdateDateOriginal value (using the annoying default formatting that WCF has used for my DateTime value), and a friendlier UpdateDate version of the same DateTime value.

    enter image description here

    I've posted the code to do this in the following article:

    Change default date serialization in WCF

    0 讨论(0)
  • 2020-12-01 11:26

    According to this msdn link, DateTime objects are...

    ...represented in JSON as "/Date(number of ticks)/". The number of ticks is a positive or negative long value that indicates the number of ticks (milliseconds) that have elapsed since midnight 01 January, 1970 UTC.

    So you are correct that .NET assumes, but it's UTC instead of GMT (though they are similar). There are some good answers here on SO that give more details and also provide methods for parsing the JSON into a usable date on the client.

    As far as converting dates from UTC to a specific time zone, on the server you could use the TimeZoneInfo class which has a ConvertTimeFromUtc method. Or you could write a custom converter that inherits from the JavaScriptConverter class. In javascript, there are the UTC and getTimezoneOffset methods that could be used.

    Hope this helps and good luck.

    0 讨论(0)
  • 2020-12-01 11:32

    Try this:

        function getDate(datestr) {
            return  new Date(eval('new ' + datestr.replace(/\//g, '')));
        }
    
    0 讨论(0)
  • 2020-12-01 11:37

    We produce data.js as a JavaScript client for OData services. If you're working from a Web client, using this library will remove this headache as well as prevent you from running into others.

    Data.js handles all of the JSONP and other concerns on your behalf, making requesting and parsing JSON data this easy:

    OData.read( 
      "http://services.odata.org/Northwind/Northwind.svc/Categories", 
      function (data) { 
        var html = ""; 
        $.each(data.results, function(l) { html += "<div>" + l.CategoryName + "</div>"; }); 
        $(html).appendTo($("#target-element-id")); 
      } 
    );
    
    0 讨论(0)
  • 2020-12-01 11:40

    If you're parsing WCF JSON date responses in Javascript, the Moment.js date framework removes much of the headache: Moment.js - Parsing ASP.NET JSON Dates. It also has some other handy methods.

    0 讨论(0)
提交回复
热议问题