Specify datetime format for WCF Data Service

最后都变了- 提交于 2019-12-11 03:08:21

问题


Is there a way to specify the formatting of DateTime data returned by a WCF data service?

This question seems to say that the default format, which looks like /Date(1339515455030)/, is the number of milliseconds since midnight Januery 1, 1970 UTC. I can figure out how to parse that if I have to but I'd rather specify a more friendly format.


回答1:


The OData DateTime format (assuming you haven't already formatted it out to a string) is dependent on the wire format negotiated between client and server. The format above is from JSON Verbose and corresponds to a standard JavaScript Date object. The Atom format is different, and JSON Light is different still. Of special note is that we decided to move away from the traditional JavaScript Date format in favor of ISO 8601 for JSON Light. In any of these cases, if you're using a client such as the WCF DS client or data.js, the client will take care of deserializing the value properly.

  • Atom formatted date: <d:OrderDate m:type="Edm.DateTime">1996-07-04T00:00:00</d:OrderDate>
  • JSON verbose formatted date: "OrderDate":"\/Date(836438400000)\/"
  • JSON light formatted date: "ShippedDate":"1996-07-16T00:00:00"

For current client libraries on a variety of platforms, see here: http://www.odata.org/libraries




回答2:


With OData V3 the JSON Verbose format uses the ISO 8601 which seems to be more friendly to some consumers. Note though that it will only be used if the payload is V3. So you will need to update your server side to supper OData V3 (if it's WCF DS, then you want to use the WCF DS 5.0 release which can be found for example on NuGet http://blogs.msdn.com/b/astoriateam/archive/2012/05/18/wcf-data-services-5-0-1-released.aspx).

Once you have that you might need to force the server to use V3 in the response payload since by default it will use the lowest possible version for the response. This can be done by sending a MinDataServiceVersion: 3.0; header with the request from the client.

After this even JSON Verbose payloads should use the ISO date time format.




回答3:


I believe that you should be able to feed the string value sent back into the datetime.parse(http://msdn.microsoft.com/en-us/library/1k1skd40.aspx) and shouldn't have to worry about the rest

Here is an example usage: http://www.dotnetperls.com/datetime-parse



来源:https://stackoverflow.com/questions/11442836/specify-datetime-format-for-wcf-data-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!