0

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

Prasad
  • 1,544
  • 5
  • 23
  • 39
Stian Bakken
  • 643
  • 1
  • 5
  • 15
  • Here is what you can do to change a string to a date and compare `ng-repeat="obj in objects" ng-if="Date.parse(obj.checkdate) >= TODAY"` if this is your problem. – Tim Mar 06 '16 at 14:39

2 Answers2

1
$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>   
Slava N.
  • 596
  • 4
  • 6
  • Maybe I should've been more thorough when describing this. When a new entry is created, the checkdate field is populated by "new Date()". But it gets saved in the database as a string; like so: 2016-03-04T09:25:57.882Z So this example will not work. Maybe I should create a function which strips the result after the date and compare only that? – Stian Bakken Mar 06 '16 at 14:05
0

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}
}
Community
  • 1
  • 1
uksz
  • 16,691
  • 26
  • 76
  • 142