OrderBy Date values, which are just strings in Angular JS
Because orderBy
filter, you can use that. Your ng-repear would probably look similar to this:
ng-repeat="item in items | orderBy: orderByDate"
and then on your controller you would define the orderByDate
function:
$scope.orderByDate = function(item) { var parts = item.dateString.split('-'); var date = new Date(parseInt(parts[2], parseInt(parts[1]), parseInt(parts[0])); return date;};
Implementation of the function is up to you. I choose to create Date
from the string. The object you return from orderByDate
function is then used to be order with using <, =, > operators.
EDIT: solution for :reverse
Since :reverse
can't be used with a function passed as parameter, you can implement your custom function to return reversed value itself. In such case using Date
is not possible, I would construct a number then and returned it with minus:
$scope.orderByDate = function(item) { var parts = item.dateString.split('-'); var number = parseInt(parts[2] + parts[1] + parts[0]); return -number;};
var now = Date.now();now = $filter('date')(now, 'yyyy-MM-ddThh:mm:ss+hh:mm');
Then you can data filtering with corresponding formatted date