1

I have an array of objects in my Angular app and each has a "ready" field, which is a timestamp. I want to count the number of objects where the ready timestamp is earlier than the current time. How would I do this?

I have:

$scope.getDatetime = new Date();
$scope.numberReady = $filter('filter')($scope.array, {ready <  $scope.getDatetime}).length;

Obviously I can't use ready < $scope.getDatetime, but that, logically speaking, is what I want to do.

1 Answer 1

2

You can use pure ES5 filter method, no need of Angular here:

$scope.getDatetime = new Date();
$scope.numberReady = $scope.array.filter(function(obj) {
    return obj.ready < $scope.getDatetime;
}).length;

... although you could use Angular filter here too:

$scope.getDatetime = new Date();
$scope.numberReady = $filter('filter')($scope.array, function(obj) {
    return obj.ready <  $scope.getDatetime;
}).length;

but since it's just a wrapper around native thing, this is not ideal in this case.

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks, this works. I'm actually having a problem comparing the datetimes though because new date() when printed to the console gives me "Sun Aug 02 2015 13:11:45 GMT+0300 (EEST)", but the datimes coming from my api are in the format "2015-07-26T21:41:01.938+03:00". Any idea how to make these comparable?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.