**
处理Json数据中的日期类型.如/Date(1415169703000)/格式
在asp.net mvc后台返回到视图中的json数据中想对数据进行操作,发现日期类型无法直接进行操作,需要转换为指定格式才行.在网上也搜了下方法也很多,觉得有点麻烦,最终使用正则搞定了,分享下:
var jsondate="/Date(1415169703000)/";
var formatdate=eval(jsondate.replace(/\/Date\((\d+)\)\//gi, "new Date($1)"));
alert(formatdate.toLocaleDateString());
有时取得的日期可能为这种类型"/Date(1415169703000+0800)/",这时代码就需要调整下了:
var jsondate="/Date(1415169703000+8000)/";
var formatdate=eval(jsondate.replace(/\/Date\((\d+)([\+\-](\d\d)(\d\d))?\)\//gi, "new Date($1)"));
alert(formatdate.toLocaleDateString());
运行结果如下:

**
本文介绍了一种在ASP.NET MVC中处理特殊格式JSON日期(如/Date(1415169703000)/)的有效方法,通过正则表达式和eval函数将日期转换为可操作的格式,适用于不同格式的日期字符串。

被折叠的 条评论
为什么被折叠?



