问题描述
可能的重复:
如何格式化 JSON 日期?
我的网络服务将 DateTime 返回给 jQuery 调用.服务以这种格式返回数据:
My webs service is returning a DateTime to a jQuery call. The service returns the data in this format:
/Date(1245398693390)/
如何将其转换为 JavaScript 友好的日期?
How can I convert this into a JavaScript-friendly date?
推荐答案
返回的是自纪元以来的毫秒数.你可以这样做:
What is returned is milliseconds since epoch. You could do:
var d = new Date();
d.setTime(1245398693390);
document.write(d);
关于如何完全按照您的需要设置日期格式,请参阅 Date 参考noreferrer">http://www.w3schools.com/jsref/jsref_obj_date.asp
On how to format the date exactly as you want, see full Date
reference at http://www.w3schools.com/jsref/jsref_obj_date.asp
您可以通过解析整数(如此处建议)来去除非数字:
You could strip the non-digits by either parsing the integer (as suggested here):
var date = new Date(parseInt(jsonDate.substr(6)));
或者应用以下正则表达式(来自评论中的 Tominator):
Or applying the following regular expression (from Tominator in the comments):
var jsonDate = jqueryCall(); // returns "/Date(1245398693390)/";
var re = /-?d+/;
var m = re.exec(jsonDate);
var d = new Date(parseInt(m[0]));
这篇关于将 .NET 日期时间转换为 JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!