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

查看:18
本文介绍了如何处理从 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");
 }

推荐答案

根据 这个msdn 链接DateTime 对象是...

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

...在 JSON 中表示为 "/Date(number滴答数)/".滴答数是一个正或负长值表示刻度数(毫秒)已经过去了UTC 时间 1970 年 1 月 1 日午夜.

...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 类,它有一个 ConvertTimeFromUtc 方法.或者您可以编写一个继承自 JavaScriptConverter 类.在 javascript 中,有 UTCgetTimezoneOffset 可以使用的方法.

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天全站免登陆