Reputation: 1061
I am passing a JSON string back from my Web Service and for some reason it is returning /Date(1461106800000+0100)/
.
I went the date to be returned in the format "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?
Upvotes: 0
Views: 3760
Reputation: 1887
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. Then, throughout the rest of the application, you can use the built in filters as and when.
Upvotes: 1