Reputation: 2179
I'm trying to filter an ng-repeat using a date range.
So far I've installed the Daterangepicker and it will return an object with two properties (startDate and endDate) in the ng-model of the input that triggers the picker (ng-model="dates"
).
It looks like this:
Object
endDate:
k_d: Mon Nov 03 2014 23:59:59 GMT+0300 (E. Africa Standard Time)
_isAMomentObject: true
_isUTC: false
_isValid: true
_locale: j
_pf: Object_
_proto__: k
startDate: k_d: Sat Oct 04 2014 00:00:00 GMT+0300 (E. Africa Standard Time)
_isAMomentObject: true
_isUTC: false
_isValid: true
_locale: j
_pf: Object_
_proto__: k
On the other hand I have my ng-repeat, that lists an array of objects, each one of them with a date
property.
<div class="row msf-row"
ng-repeat="record in recordlist | filter:dateFilter | limitTo : -100">
<div class="col-md-1">{{record.date}}</div>
</div>
I've built a filter to try to filter it by range:
$scope.dateFilter = function (record) {
return record.date >= $scope.dates.startDate && record.date <= $scope.dates.endDate;
};
But so far the filter is not working. What am I missing?
EDIT
I've realized that record.date
is a string while startDate and endDate are moment() objects. Is there any way I could parse record.date
into a moment() object before the filter function?
EDIT 2
Fixed it! I had to parse the result.date
as a momentjs object, and also to include the proper format like this: moment(string, format)
:
$scope.dateFilter = function (record) {
return
moment(record.date, "DD[/]MM[/]YYYY") >= $scope.dates.startDate
&&
moment(record.date, "DD[/]MM[/]YYYY") <= $scope.dates.endDate;
};
Upvotes: 0
Views: 2077
Reputation: 359
Looking at your date object, it seems like it is not of JavaScript native 'Date' type. May be something on this line works?
$scope.dateFilter = function (record) {
var startDate = new Date($scope.dates.startDate.k_d);
var endDate = new Date($scope.dates.endDate.k_d);
return record.date >= startDate && record.date <= endDate;
};
Sharing a jsfiddle may be more helpful to figure out exact issue.
Upvotes: 3