将.NET DateTime转换为JSON [英] Converting .NET DateTime to JSON

查看:88
本文介绍了将.NET DateTime转换为JSON的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

如何格式化JSON日期?

我的web服务将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 参考资料,网址为 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 DateTime转换为JSON的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆