簡體   English   中英

AngularJS 過濾數據,日期為今天、周和月

[英]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
  }
]

我有 3 個錨按鈕(今天、周、月)我想使用 ng-cick = GetDate(Today or Week or Month) 過濾此數據並綁定數據

如何根據我點擊的內容過濾 $scope.Data(今天、周和月)

請幫忙

為了過濾數據,您可以執行以下操作:

// 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);

並綁定適用於按鈕點擊。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM