2017-02-24 160 views
0

我有一個對象數組,其中有一個date_time屬性,在此數組中,我希望獲得更早使用當前date_time的date_times對象的長度。獲取比當前日期時間晚的日期數

例:

[data:[{date: "2017-02-24 16:41:51"}, {date: ""2017-02-21 16:41:51"}...], 
last_clicked: "2017-02-24 19:41:51"] 

我希望得到的「數據」數組,一個富人比DATE_TIME「last_clicked」越早objecs的長度。

+0

目前還不清楚你問什麼。你需要獲取'data'數組的長度嗎?向我們展示您想使用日期ASC/DESC過濾「dates_times」數組 –

+0

? – alphapilgrim

+0

最好在時間戳中保存日期。因此,根據客戶時區,您可以顯示時間和日期。 –

回答

0

也許你可以嘗試這樣的事:

let length = data.filter(function(d) { 
    return new Date(d.date) < new Date(last_clicked) 
}).length; 
0

您可以使用Angulars-orderBy-filter。我爲你設置了一個例子,只需將數據的日期鍵的id鍵換掉即可。

function exampleController($scope, exampleFactory, $filter) { 
 
    $scope.list = []; 
 
    $scope.reverse = false; 
 
    $scope.changeSortOrder = function() { 
 
    $scope.reverse = !$scope.reverse; 
 
    }; 
 

 
    function getList() { 
 
    exampleFactory 
 
     .getList() 
 
     .then(function(list) { 
 
     // $scope.list = list; 
 
     //alternatively you could filter here and change the sort order with the button if needed at all. 
 
     $scope.list = $filter('orderBy')(list, '-id'); //where this would be replaced by date(note: the '-' in front of id changes the sort order ASC/DESC) 
 
     //$scope.list = $filter('orderBy')(list, 'id', true); //where 3rd argument is wether order should be reversed 
 
     }); 
 
    } 
 

 
    getList(); 
 
} 
 

 
function exampleFactory($http) { 
 
    var root = 'http://jsonplaceholder.typicode.com'; 
 

 
    function getList() { 
 
    return $http.get(root + '/comments') 
 
     .then(function(resp) { 
 
     return resp.data; 
 
     }); 
 
    } 
 
    return { 
 
    getList: getList 
 
    }; 
 
} 
 

 
angular 
 
    .module('app', []) 
 
    .controller('exampleController', exampleController) 
 
    .factory('exampleFactory', exampleFactory);
.container-fluid { 
 
    background-color: #1D1F20; 
 
    color: #fff; 
 
    font-size: 14px; 
 
    font-weight: bold; 
 
    button { 
 
    margin-top: 20%; 
 
    } 
 
} 
 

 
ul li { 
 
    list-style: none; 
 
    margin: 10px; 
 
} 
 

 
.child-padding>div { 
 
    padding: 2px; 
 
} 
 

 
.col-md-2 { 
 
    position: fixed; 
 
    button { 
 
    margin-bottom: 10%; 
 
    } 
 
} 
 

 
.btn-circle { 
 
    width: 30px; 
 
    height: 30px; 
 
    text-align: center; 
 
    padding: 6px 0; 
 
    font-size: 12px; 
 
    line-height: 1.428571429; 
 
    border-radius: 50%; 
 
}
<link href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/> 
 
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.5.6/angular.min.js"></script> 
 

 
<div class="container-fluid" ng-app="app"> 
 
    <div class="container" ng-controller="exampleController"> 
 
    <div class="row"> 
 
     <div class="col-md-2 text-center"> 
 
     <button class="btn btn-primary" type="button" ng-click="changeSortOrder()">Change Sort Order</button> 
 
     </div> 
 
     <div class="col-md-10 pull-right"> 
 
     <ul class="ul"> 
 
      <li ng-repeat="comment in list | orderBy: 'date': reverse track by $index"> 
 
      <div class="child-padding"> 
 
       <div> 
 
       <span ng-bind="comment.email"></span> 
 
       <span class="pull-right btn-info btn-circle" ng-bind="comment.id"></span> 
 
       </div> 
 
       <div ng-bind="comment.body"></div> 
 
      </div> 
 
      <div ng-bind="comment.name"></div> 
 
      </li> 
 
     </ul> 
 
     </div> 
 
    </div> 
 
    </div> 
 
</div>

0

使用Date.parse解析您的日期字符串

DEMOJsFiddle

下面的代碼將提醒:

早晚時間:1

var o = { 
data:[ 
    {date: "2017-02-24 16:41:51"}, 
    {date: "2017-02-21 16:41:51"} 
], 
last_clicked: "2017-02-24 12:41:51" 
}; 

function getSoonerLength(data, soonerThan) { 
    var count = 0; 
    for (var i = 0 ; i < data.length ; i++) { 
    var d = Date.parse(data[i].date); 
    var than = Date.parse(soonerThan); 
    if (d < than) 
     count++; 
    } 
    return count; 
} 

alert('Sooner times: ' + getSoonerLength(o.data, o.last_clicked)) 
相關問題