How to Convert .NET DateTime to JSON

狂风中的少年 提交于 2020-01-21 12:48:18

问题


My WCF webservice is returning C# DateTime objects as JSON like this,

"/Date(1293793200000+1300)/"

I have found this,

Converting .NET DateTime to JSON

Which suggests this method for converting it using javascript,

var d = new Date();
d.setTime(1245398693390);
document.write(d);

The difference is that my date format has the +1300 in it, which seems to be my timezone, as I am +13 hours from GMT.

Can I somehow modify my service to adjust the value to an absolute number of milliseconds from epoch or can the javascript be modified?


回答1:


You can ignore the time zone offset - the number of milliseconds is always relative to UTC (see the "DateTime wire format" section at http://msdn.microsoft.com/en-us/library/bb412170.aspx). The offset mostly indicates that the original DateTime value was of DateTimeKind.Local, but the serialized milliseconds since the epoch is always relative to GMT.




回答2:


You can convert the DateTime object in .NET to a string and just serialize the string. That's what I did. See Proper way to format date from database using javascript/jquery



来源:https://stackoverflow.com/questions/8843441/how-to-convert-net-datetime-to-json

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