如何按未来和过去日期过滤数据

时间:2016-11-22 05:32:52

标签: angularjs

  

嗨,我是棱角分明的新手,我想通过过去的数据显示数据   昨天的日期,显示今天+未来日期的数据。

//Here is the HTML code for Past Events
<div ng-repeat="event in events | filter:pastDate">
<p>{{event.name}}
<div>

//Here is the HTML code for Future Events
<div ng-repeat="event in events | filter:futureDate">
<p>{{event.name}}
<div>
  

每个活动都有一个开始日期和结束日期,   任何人都可以解释如何实现这一目标。

1 个答案:

答案 0 :(得分:0)

试试这个

var myApp = angular.module('app', []);
myApp.controller('TestController', ['$scope', function($scope) {
    var d = new Date();        
    d.setDate(d.getDate() - 1);
    $scope.events = [{date:new Date}, {date: new Date()},{date: d}];     
    prepareDate = function() {
      var date = new Date();
      date.setHours(0);
      date.setMinutes(0);
      date.setSeconds(0);
      return date;
    }  
    $scope.pastDate = function(actual){
      return actual.date < prepareDate();        
    }
    $scope.futureDate = function(actual){
      return actual.date >= prepareDate();        
    }
}]);
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<body ng-app="app">
  <div ng-controller="TestController">
    <h4>Past:</h4>
    <div ng-repeat='event in events | filter : pastDate'>{{event.date| date:'yyyy-MM-dd HH:mm:ss'}}</div>
    <h4>Future:</h4>
    <div ng-repeat='event in events | filter : futureDate'>{{event.date| date:'yyyy-MM-dd HH:mm:ss'}}</div>
</div>
</body>