如何处理从WCF数据服务(OData)返回的json DateTime [英] How to handle json DateTime returned from WCF Data Services (OData)

查看:153
本文介绍了如何处理从WCF数据服务(OData)返回的json DateTime的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我相信我在这里缺少明显的东西.当我从OData服务请求JSON响应时,DateTime属性得到的结果与请求XML时得到的结果不同.我将以NerdDinner OData提要为例.

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>

当我执行警报(新日期(1235764800000))时,我得到以下结果:

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

当我使用LINQPad运行相同的查询时,我也得到了8PM的结果. 为什么时区在JSON结果中不正确?似乎假设响应是在GMT中进行的.我应该在客户端上处理此问题(通过JavaScript)还是在服务器上进行设置?

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?

我在客户端上使用jQuery,在服务器上使用WCF数据服务(和实体框架).

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

更新:

我在客户端使用 Datejs 来处理UTC日期时间格式.我想知道这是否是解决此问题的正确方法.

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");
 }

推荐答案

根据

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

...在JSON中表示为"/Date(number 的滴答声)/.滴答声的数量是 正或负的long值 指示刻度数 从那以后经过的(毫秒) 1970年1月1日午夜(UTC).

...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.

.NET假设您是正确的,但是它是UTC而不是GMT(尽管一些 答案,其中提供了更多详细信息,还提供了将JSON解析为可用日期的方法客户.

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.

只要将日期从UTC转换为特定时区,就可以在服务器上使用 TimeZoneInfo 类,该类具有 JavaScriptConverter 类.在javascript中,有 UTC

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.

这篇关于如何处理从WCF数据服务(OData)返回的json DateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆