Filtro por Data para Angular JS

by Ivan Ferrer

HTML

<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css">
<script src="https://code.jquery.com/jquery-latest.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/angular-i18n/1.4.6/angular-locale_pt-br.min.js"></script>
<div ng-app="MyApp" class="container">
    <div ng-controller="MyCtrl"style="padding-top:10px;">
        <div class="row">
            <div class="form-inline" style="margin-left:20px;">
                <div class="form-group">
                <label class="item item-input">Inicial: </label> 
                  <input type="date" class="form-control" placeholder="Data Inicial"  name="startDate" min-date="2011-01-01" ng-model="startDate">
                </div>
                <div class="form-group">
                <label class="item item-input">Final: </label>
                  <input type="date" class="form-control" placeholder="Data Final" name="endDate" min-date="2011-01-01" ng-model="endDate">
                </div>
           </div>
        </div>
          <br>
            <table class="table">
              <h3>Prestadores</h3>
              <thead>
                <tr>
                  <th>Débito</th>
                  <th>Glosa</th>
                  <th>Líquido</th>
                  <th>Data</th>  
                </tr>
              </thead>
              <tbody>
                <tr ng-repeat="item in items | betweenDate:'dataPagamento':startDate:endDate" ng-if="items.length > 0">
                  <td>{{item.totalDebito | currency:"R$ ":0}}</td>
                  <td>{{item.totalGlosa | currency:"R$ ":0}}</td>
                  <td>{{item.totalLiquido | currency:"R$ ":0}}</td>
                  <td>{{item.dataPagamento | date:"dd/MM/yyyy"}}</td>
                </tr>
                  <tr ng-if="(items|betweenDate:'dataPagamento':startDate:endDate).length == 0 && items.length != 0" class="text-center"><td colspan="4">Não há dados para esse período.</td></tr>
               ...

JavaScript

var app = angular.module('MyApp', []);
app.filter('betweenDate', function($filter) {
   return function(collection, column, startDate, endDate) {
        var new_collection = [];
    if (angular.isDefined(startDate) && angular.isDefined(endDate)) {
       if (startDate != '' && endDate != '') {
          if (angular.isDefined(startDate)) {
             startDate = Date.parse($filter('date')(startDate, 'yyyy-MM-dd HH:mm:ss'));
          }
          if (angular.isDefined(endDate)) {
             endDate = Date.parse($filter('date')(endDate, 'yyyy-MM-dd HH:mm:ss'));
          }
          if (!isNaN(startDate) && !isNaN(endDate)) {
              angular.forEach(collection, function (value, index) {
              var obj = value[column];
              var currentDate = Date.parse($filter('date')(obj, 'yyyy-MM-dd HH:mm:ss'));
                 if ((currentDate >= startDate &&  endDate >= currentDate)) {
                    new_collection.push(value);         
                 }
              });
          } else {
            new_collection = collection;
          }
      } else {
        new_collection = collection;
      }
      collection = new_collection;
    }
  return collection;
  };
  
}); 

app.controller('MyCtrl', function($scope, $filter) {
   
    $scope.items = [
                    {
                        "codigoPrestador":9520,
                        "nomePrestador":"Diego",
                        "dataPagamento":"2015-04-30T00:00:00-0300",
                        "totalDebito":6327.59,
                        "totalLiquido":96560.2,
                        "totalLote":102886,
                        "totalGlosa":52.71
                    },
                    {
                        "codigoPrestador":9520,
                        "nomePrestador":"Diego",
                        "dataPagamento":"2015-07-15T00:00:00-0300",
                        "totalDebito":5600.41,
                        "totalLiquido":85463.2,
                       ...