简体   繁体   中英

AngularJS Filter Data with Date as Today , Week and Month

$scope.Data = [
{
    "title": "Algolia's Global Roadshow",
    "author": "Ryan Chen",
    "excerpt": "We've heard it from experts, industry surveys, and our most successful customers: search and discovery are key to moving the digital conversation forward.",
    "date": "2018-10-17",
    "date_timestamp": 1539734400
  },
  {
    "title": "Black Friday & Site Search: small tips, big difference",
    "author": "Matthieu Blandineau",
    "excerpt": "It’s no surprise that during the holiday season, Black Friday & Cyber Monday are absolutely critical events for any e-commerce business. According to Adobe, online retailers earned $108.15B between Nov 1 and Dec 31 2017, up 13.8% from 2016. Only in the U.S.",
    "date": "2018-10-05",
    "date_timestamp": 1538697600
  },
  {
    "title": "For better school projects, a partnership with GitHub",
    "author": "Jessica West",
    "excerpt": "Hello GitHubbers and Algolians alike! We have some exciting news we’d like to share with you. Algolia is so pleased to announce that we have partnered with GitHub’s Student Developer Pack to help students build search functionality into their projects freely and effortlessly 🎉.",
    "date": "2018-09-18",
    "date_timestamp": 1537228800
  }
]

I have 3 Anchor Button ( Today , Week , Month ) I want to Filter this data using ng-cick = GetDate(Today or Week or Month) and Bind the data

How to filter the $scope.Data based on what i click ( Today , Week and Month )

Please help

For filtering the data you can do something along the lines of:

// Set the start and end dates for the search. Limit to one day, week, etc.
const startDate = new Date(2018, 9, 4);
const endDate = new Date(2018, 9, 6);

// Filter by comparing the dates
const filteredData = $scope.Data.filter(e => {
    const date = new Date(e.date);
    return date.getTime() >= startDate.getTime() && date.getTime() <= endDate.getTime();
});

console.log(filteredData);

And bind as applicable to the button clicks.

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