Skip to content Skip to sidebar Skip to footer

How To Format A JSON Date In AngularJS

Issue 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'

Solution 1:

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.


Post a Comment for "How To Format A JSON Date In AngularJS"