使用Angular显示今天的生日

时间:2016-05-10 14:17:35

标签: angularjs

我想从团队列表中显示今天的生日。我的数据包含他们的生日(例如16/12/1999)和姓名。我用这个来展示即将到来的出生。但我想显示今天生日块。请帮忙

<script>
function birthdayCtrl($scope){

$scope.friends=[
  {name:'Pete','birthday':"12/25/1983"},
  {name:'Sarah','birthday':"02/14/1985"},
  {name:'James','birthday':"01/15"},
  {name:'Lilly','birthday':"04/01/1975"},
  {name:'John','birthday':"08/07/1955"},

  ]

  $scope.friends.forEach(function(data){
      var day = data.birthday.split("/")
      var currentYear = new Date().getFullYear();
      var birthdayDate = new Date(currentYear, day[0] - 1, day[1])
      var now = new Date().valueOf();
      if (birthdayDate.valueOf() < now){ 
          birthdayDate.setFullYear(currentYear+1)
      }
      data.fromNow = birthdayDate.valueOf() - now;
  })
}

</script>
<body ng-controller="birthdayCtrl">
    <h1>Upcoming Birthdays</h1>
    <table>
    <th>No.</th>
    <th>Name</th>
    <th>Birthday</th>
    <tr ng-repeat="friend in friends| orderBy:'fromNow' ">
    <td>{{$index}}</td>
    <td>{{friend.name}}</td>
    <td>{{friend.birthday}}</td>

    </tr>
    </table>
    <style>
     table td,table th{
    border: thin solid #eaeaea;
    width:150px;
    padding:3px 5px;
    </style>
  </body>

1 个答案:

答案 0 :(得分:1)

有几种方法可以做到这一点。一种方法是使用ng-if="todayBirthday ? onlyToday(friend) : true"http://plnkr.co/edit/kMGIlNcESjg4kYE3oHhY?p=preview

$scope.onlyToday = function(friend) {
  var today = new Date();

  if(friend.birthDate.getDate() === today.getDate() && friend.birthDate.getMonth() === today.getMonth())
    return true;
  else
    return false;
}