AngularJS處理ASP.Net MVC Json返回日期
阿新 • • 發佈:2019-01-06
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自帶的過濾器。