AngularJS - How to filter date in controller
-
21-12-2019 - |
Question
I have a JSON file returning a number of dates in such format:
/Date(1235548800000)/
How can i filter this in my Controller to only return the year?
I have tried:
var filteredDate = $filter('date')($scope.ToDate, 'yyyy');
console.log(filteredDate);
But console is displaying:
/Date(1235548800000)/
Solution
You could try this
var parsedDate = new Date(parseInt($scope.ToDate.substr(6)));
var filteredDate = $filter('date')(parsedDate, 'yyyy');
OTHER TIPS
Check my Plunker. You should get the timestamp from ToDate
string. You can use something like this if all the time date has the same format.
var dt=$scope.toDate.substring($scope.toDate.indexOf("(")+1,$scope.toDate.indexOf(")"));
Try this:
var filteredDate = YourDate.getFullYear();
console.log(filteredDate);
Hope it helps...........!
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow