How to handle json DateTime returned from WCF Data

2019-01-09 00:47发布

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 request XML. I'll use the NerdDinner OData feed as an example.

JSON:

http://www.nerddinner.com/Services/OData.svc/Dinners(1)?$format=json
"EventDate": "\/Date(1235764800000)\/"

XML:

http://www.nerddinner.com/Services/OData.svc/Dinners(1)
<d:EventDate m:type="Edm.DateTime">2009-02-27T20:00:00</d:EventDate>

When I do an alert(new Date(1235764800000)) I get this result: alt text

I also get a result of 8PM when I run the same query with LINQPad. Why is the time zone incorrect in the JSON result? It seems to assume that the response is in GMT. Should I handle this on the client (via javascript) or is this something that I can set on the server?

I'm using jQuery on the client and WCF Data Services (and Entity Framework) on the server.

Update:

I am using Datejs on the client side to handle the UTC datetime formatting. I'm wondering if this is the correct way to go about this problem.

 function getDateString(jsonDate) {
     if (jsonDate == undefined) {
         return "";
     }
     var utcTime = parseInt(jsonDate.substr(6));

     var date = new Date(utcTime);
     var minutesOffset = date.getTimezoneOffset();

     return date.addMinutes(minutesOffset).toString("M/d/yyyy h:mm tt");
 }

8条回答
三岁会撩人
2楼-- · 2019-01-09 01: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

查看更多
仙女界的扛把子
3楼-- · 2019-01-09 01:25

Try this:

    function getDate(datestr) {
        return  new Date(eval('new ' + datestr.replace(/\//g, '')));
    }
查看更多
地球回转人心会变
4楼-- · 2019-01-09 01:36

If this may help, I was facing the same problem and I ended to implement something like this, not so elegant but it works.

String.prototype.DateWCF = function(dateformat) {
    return new Date(parseInt(this.match(/\/Date\(([0-9]+)(?:.*)\)\//)[1])).format(dateformat);
};

then on $.ajax success:

        success: function(data) {
            $.each(data, function() {
                var hello = this.DateTimeProperty.DateWCF('dd-MM-yyyy'));
            });
        }

I hope this may be helpful.

查看更多
家丑人穷心不美
5楼-- · 2019-01-09 01:42

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")); 
  } 
);
查看更多
Rolldiameter
6楼-- · 2019-01-09 01:46

Using date.js script.Try below

new Date(parseInt(yourDateValue)).toString("ffffd, dd-MMM-yyyy, hh:mm:ss")
查看更多
女痞
7楼-- · 2019-01-09 01:46

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.

查看更多
登录 后发表回答