I was able to filter dates using the custom filter when the date was in string format.However I ran into issue while trying to filter json date object using custom filter.The json date i use was retrieved from mongodb.
Here is the format that works.It filter json date which is in string format http://plnkr.co/edit/5IhJYSXvqa5nwd87y8kD?p=preview
but when i use json date object i am not able to filter using the dates in my custom filter(plunker showing code that does not work) because there seems to be problem filtering the json date object in my custom filter` http://plnkr.co/edit/en36loBKQ2DAnOcbwe8v?p=preview
Is there a way to filter json dates in the custom filer i created
// Code goes here
// Code goes here
//http://stackoverflow.com/questions/18935889/difference-between-date-parse-and-gettime
var app = angular.module('tempfilter', []);
app.controller('MainCtrl', function($scope) {
$scope.sensordata = [{
id: 'id:1',
name: 'Rob',
"ValidationDate": {
"$date": "2015-02-20 18:00:05-0400"
},
"Temp": 42
}, {
id: 'id:3',
name: 'Rob',
"ValidationDate": {
"$date": "2015-02-23 23:00:00-0400"
},
"Temp": 42
}, {
id: 'id:4',
name: 'Bob',
"ValidationDate": {
"$date": "2015-02-23"
},
"Temp": 22
}, {
id: 'id:5',
name: 'Bob',
"ValidationDate": {
"$date": "2015-02-23T17:16:14.720Z"
},
"Temp": 50
}, {
id: 'id:6',
name: 'Don',
"ValidationDate": {
"$date": "2015-02-19 13:00:05-0400"
},
"Temp": 50
}, {
id: 'id:7',
name: 'Don',
"ValidationDate": {
"$date": "02/18/2015 13:00:05-0400"
},
"Temp": 50
}, {
id: 'id:8',
name: 'Don',
"ValidationDate": {
"$date": "02/17/2015"
},
"Temp": 50
}, {
id: 'id:9',
name: 'Don',
"ValidationDate": {
"$date": "02/16/2015"
},
"Temp": 50
}, {
id: 'id:10',
name: 'Sinclair',
"Date": new Date(),
"Temp": 65
}];
$scope.filter = {
value: 50
};
});
app.filter('tempo', function() {
return function(items, field, value) {
var filtered = [];
var newdate = new Date().setDate(new Date().getDate() - value);
angular.forEach(items, function(item) {
if (new Date(item[field]) > newdate) {
filtered.push(item);
}
});
return filtered;
};
});
<!DOCTYPE html>
<html ng-app="tempfilter">
<head lang="en">
<meta charset="utf-8">
<title>DateFilter</title>
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.0.3/angular.min.js"></script>
<script>
document.write('<base href="' + document.location + '" />');
</script>
<script src="script.js"></script>
</head>
<body ng-controller="MainCtrl">
Number of days before today
<input type="number" ng-model="filter.value">
<p id="demo">Showing data for last {{ filter.value }} days</p>
Filtered list:
<ul>
<li ng-repeat="s in sensordata | tempo:'ValidationDate.$date':filter.value">{{s.id}} {{s.ValidationDate.$date|date}} {{s.name}} {{s.Temp}}
</ul>
Full List:
<ul>
<li ng-repeat="s in sensordata ">{{s.id}} {{s.ValidationDate.$date|date}} {{s.name}} {{s.Temp}}
</li>
</ul>
</body>
</html>
`