AngularJS处理ASP.Net MVC Json返回日期

来源:互联网 发布:qt socket多线程编程 编辑:程序博客网 时间:2024/06/01 03:57

ASP.NET MVC 在返回 JSON 类型时,,时间的格式会是 "/Date(1306418993027)/"


public ActionResult Test(){    List<Person> persons = new List<Person>();    //...    return Json(persons, JsonRequestBehavior.AllowGet);}
其中Person有birthday字段(DateTime)。


返回的json中,日期格式时间的格式会是 "/Date(1306418993027)/"。


js中可以用


return new Date(parseInt(x.substr(6)));
来处理。


在AngularJS中,只要自定义filter即可。


myApp.filter("jsDate", function () {    return function (x) {        if (null == x) {            return null;        }        else {            return new Date(parseInt(x.substr(6)));        }    };});
在使用的地方


<td>{{person.birthday|jsDate| date: 'yyyy-MM-dd HH:mm:ss'}}</td>
其中date是AngularJS自带的过滤器。


0 0
原创粉丝点击