0

I have a list of items that came in a result from the API. In my view, I present this results whit an ng-repeat.

<div ng-repeat="item in items"></div>

heres my API response.

2017-01-17T09:35:38.429Z
2017-01-03T10:00:00.000Z
2017-01-27T14:34:55.179Z

I need to filter by date, i.e if the items are more than 3 months old show.

Hugo Seleiro
  • 2,467
  • 5
  • 26
  • 39

2 Answers2

0

assuming its an array:

let items = ["2017-01-17T09:35:38.429Z",
"2017-01-03T10:00:00.000Z",
"2017-01-27T14:34:55.179Z"]

do this

let threeMonthsAgo = new Date(Date.now() - 7776000000)
items = items.filter(date => { threeMonthsAgo.before( (new Date(date)) ) })
Arel Sapir
  • 136
  • 5
0

You display the item based on your condition, in your case should not be older than 3 months.

<div ng-repeat="item in items"> <div ng-if="!isOlderThan3Months(item)">item</div> </div>

function isOlderThan3Months(date){
   return new Date().getTime() - new Date(date).getTime() > 1000 /*ms*/ * 60/*s*/ * 60/*min*/ * 24/*h*/ * 30/*days*/ * 3/*months*/ )
}

Where condition can call a function to calculate the difference between dates. Look here for multiple ways to do this.

Andrew Radulescu
  • 1,862
  • 13
  • 21