简体   繁体   中英

How to filter by a property 'today' in ng-repeat using angularjs?

I have an ng-repeat on an array. Each object has a property called "checkdate" which is just a "New Date()" .

I want the ng-repeat to only show objects created TODAY. How can I do this?

Thanks

$scope.list1 = [
  {name: "item1", checkdate: '2016-03-04T09:25:57.882Z'},
  {name: "item2", checkdate: '2016-03-05T09:25:57.882Z'},
  {name: "item3", checkdate: '2016-03-06T09:25:57.882Z'}];

var curDate = new Date();
var y = curDate.getFullYear();
var m = curDate.getMonth() + 1;
if (m < 10) {
  m = '0' + m;
}
var d = curDate.getDate();
if (d < 10) {
  d = '0' + d;
}
$scope.curDate = y + '-' + m + '-' + d;

...

<div ng-repeat="item in list1 | filter:{checkdate: curDate}">
  {{item.name}} - {{item.checkdate}}
</div>   

Use the following:

ng-repeat="obj in objects" 

and inside:

ng-show="obj.date === Date.now()"

And modify Date.now() according to the format of your objects date format.

Like here

Or you can do the following:

ng-show="checkDate(obj.date)"

where

checkdate(date){
var todaysDate = new Date();
//call setHours to take the time out of the comparison
if(date.setHours(0,0,0,0) == todaysDate.setHours(0,0,0,0));
{
    return true
} else { return false}
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM