[英]How to format a JSON date in AngularJS
I am passing a JSON string back from my Web Service and for some reason it is returning /Date(1461106800000+0100)/
. 我从Web服务传回JSON字符串,由于某种原因,它返回
/Date(1461106800000+0100)/
。
I went the date to be returned in the format "yyyy-MM-dd"
我以
"yyyy-MM-dd"
格式返回了要返回的日期
I have been searching online and in my controller I have created a filter which looks like this: 我一直在在线搜索,并且在我的控制器中创建了一个看起来像这样的过滤器:
app.filter("dateFilter", function () {
return function (item) {
if (item != null) {
return new Date(parseInt(item.substr(6)));
}
return "";
};
});
In my controller I have written this: 在我的控制器中,我这样写:
var bens = $filter('dateFilter')(ben.GetAllEventsByUserResult.HOLIDAY_START);
That returns this: 返回以下内容:
How can I get the date in the yyyy-MM-dd format? 如何获取yyyy-MM-dd格式的日期?
try something like this, this uses the built in angular date filter with the newly parsed date. 尝试类似的方法,它使用带有新解析日期的内置角度日期过滤器。
app.filter("dateFilter", function ($filter) {
return function (item) {
if (item != null) {
var parsedDate = new Date(parseInt(item.substr(6)));
return $filter('date')(parsedDate, 'yyyy-MM-dd');
}
return "";
};
});
Edit: I'd actually recommend, instead of having to use your custom filter all the time, you can add a HTTP interceptor, that reads your response, and automatically parses the dates, navigating the whole object using recursion. 编辑:实际上,我建议您不必添加所有时间使用自定义过滤器,而可以添加HTTP拦截器,该拦截器读取响应并自动解析日期,并使用递归导航整个对象。 Then, throughout the rest of the application, you can use the built in filters as and when.
然后,在应用程序的其余部分中,您可以随时使用内置过滤器。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.