如何处理从 WCF 数据服务 (OData) 返回的 json DateTime
我相信我在这里遗漏了一些明显的东西。当我从 OData 服务请求 JSON 响应时,我得到的 DateTime 属性结果与请求 XML 时得到的结果不同。我将使用 NerdDinner OData 源作为示例。
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>
当我执行警报(new Date(1235764800000)) 时,我得到以下结果:
当我使用 LINQPad 运行相同的查询时,我也得到了晚上 8 点的结果。 为什么 JSON 结果中的时区不正确? 似乎假设响应是 GMT。我应该在客户端上处理这个问题(通过javascript)还是可以在服务器上设置?
我在客户端上使用 jQuery,在服务器上使用 WCF 数据服务(和实体框架)。
更新:
我在客户端使用 Datejs 来处理 UTC日期时间格式。我想知道这是否是解决这个问题的正确方法。
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");
}
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:
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");
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(8)
我们生产 data.js 作为 OData 服务的 JavaScript 客户端。如果您使用 Web 客户端工作,使用此库将消除这个令人头疼的问题,并防止您遇到其他人。
Data.js 代表您处理所有 JSONP 和其他问题,使请求和解析 JSON 数据变得如此简单:
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:
试试这个:
Try this:
此回复可能会被否决 (!!),但另一种解决方案是更改您的 WCF 服务以更友好的方式返回日期。
下面是来自我的 WCF 服务的一些示例 JSON,显示了
UpdateDateOriginal
值(使用 WCF 用于我的 DateTime 值的烦人的默认格式),以及更友好的UpdateDate
版本相同的日期时间值。我已在以下文章中发布了执行此操作的代码:
更改 WCF 中的默认日期序列化
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 friendlierUpdateDate
version of the same DateTime value.I've posted the code to do this in the following article:
Change default date serialization in WCF
根据 此 msdn 链接,
DateTime
对象是...所以.NET假设你是正确的,但它是UTC而不是GMT(尽管 他们类似)。有一些好 答案在这里提供更多详细信息,并提供将 JSON 解析为可用日期的方法客户。
至于将日期从 UTC 转换为特定时区,在服务器上您可以使用
TimeZoneInfo
类,该类具有ConvertTimeFromUtc
方法。或者您可以编写一个继承自JavaScriptConverter
类。在 javascript 中,有UTC
和getTimezoneOffset
可以使用的方法。希望这有帮助,祝你好运。
According to this msdn link,
DateTime
objects are...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 aConvertTimeFromUtc
method. Or you could write a custom converter that inherits from theJavaScriptConverter
class. In javascript, there are theUTC
andgetTimezoneOffset
methods that could be used.Hope this helps and good luck.
如果这可能有帮助,我面临着同样的问题,我最终实现了这样的东西,不是那么优雅,但它有效。
然后在
$.ajax
成功:我希望这可能会有所帮助。
If this may help, I was facing the same problem and I ended to implement something like this, not so elegant but it works.
then on
$.ajax
success:I hope this may be helpful.
这应该可以正常工作:
substr 函数取出“/Date(”部分,parseInt 函数获取整数并忽略末尾的“)/”。
对于 ISO-8601 格式的 JSON 日期,只需将字符串传递到 Date 构造函数中即可:
这已经被修复并讨论了,请查看此 上一篇文章
This should work just fine:
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:
This was already fixed and discussed that a look at this previous post
使用 date.js 脚本。尝试下面
Using date.js script.Try below
如果您在 Javascript 中解析 WCF JSON 日期响应,Moment.js 日期框架可以消除很多麻烦:Moment.js - 解析 ASP.NET JSON 日期.它还有一些其他方便的方法。
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.