javascript - 使用 yyyy/mm/dd 格式以 ng-repeat 显示本周数据

标签 javascript angularjs mean-stack

有没有办法使用日期字符串格式限制 ng-repeat 中的显示?我正在尝试使表中的某些数据过期,例如:如果日期是 2019/03/22.. 它不会显示在表中,因为它是上周的数据

angular.module('selectExample', [])
  .controller('ExampleController', ['$scope', function($scope) {
    $scope.register = {
      regData: {
        branch: {},
      },
      names: [
        {name:"narquois",date:"2019/03/30"},
        {name:"vorpal",date:"2019/03/28"},
        {name:"keen",date:"2019/03/25"},
        {name:"argol",date:"2019/03/18"},
        {name:"long",date:"2019/03/17"},
        {name:"propolis",date:"2019/03/16"}
      ],
    };
  }]);
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular.js/1.7.5/angular.min.js"></script>
<div ng-app="selectExample" ng-controller="ExampleController">
<table id="example" width="100%">
    <thead>
       <tr align="center">
         <th>Name</th>
         <th>Date</th>
       </tr>
    </thead>			
    <tbody>
       <tr ng-repeat="person in register.names">
         <td align="center">{{ person.name }}</td>
         <td align="center">{{ person.date }}</td>
       </tr>
    </tbody>
</table> 
</div>

最佳答案

您可以创建一个自定义过滤器,您可以在其中获取一周的第一天和最后一天,并检查从数组传递的日期是否在该范围内。

js

$scope.filterCurrentWeek = function(row){   
  var today = new Date(); // get current date
  var first = today.getDate() - today.getDay();
  var last = first + 6;

  var firstDay = new Date(today.setDate(first)).toUTCString();
  console.log(firstDay)
  var lastDay = new Date(today.setDate(last)).toUTCString();
  console.log(lastDay)
  var rowDate = new Date(row.date)
  console.log(rowDate)

  if(Date.parse(firstDay) <= Date.parse(rowDate) && Date.parse(lastDay) >= Date.parse(rowDate)){
    return true;
  }
  else{
    return false;
  }

}

html

<table id="example" width="100%">
<thead>
   <tr align="center">
     <th>Name</th>
     <th>Date</th>
   </tr>
</thead>            
<tbody>
   <tr ng-repeat="person in register.names | filter: filterCurrentWeek">
     <td align="center">{{ person.name }}</td>
     <td align="center">{{ person.date }}</td>
   </tr>
</tbody>
</table> 

Demo

关于javascript - 使用 yyyy/mm/dd 格式以 ng-repeat 显示本周数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55430789/

相关文章:

javascript - 复选框更改在 html 表和 jquery 内部无法正常工作

javascript - Transpile ES2015 给出 `export` 错误

JavaScript innerHTML 只会在循环结束时工作

angularjs - Protractor 找不到之前已找到 2 个测试的元素

angularjs - 服务与工厂的区别

javascript - 如何在 Protractor 中查找元素定位器

javascript - 将数据渲染成 Angular 时遇到问题

html - 我的 node.js 服务器在 intelliJ 中运行,但 localhost 只提供一个空白页面

javascript - 水平滚动和垂直滚动

node.js - 如何在 MEAN 堆栈中使用 orientDB