Trying to compare the new Date(today's date) in 2015-02-21T21:48:48.137Z format with date in MM/DD/YYYY format which is my json data. I would like to filter all the data before today's days based on user input based on number of day's user enters(getting data before no certain number of days)..I can get the number of days and subtract it from todays date. Don't know how to compare this date with Json date value in MM/DD/YYYY format.Should i parse the Date() in order to compare with JSON data in MM/DD/YYYY format
I am able to take user input and subtract it from today's date based on 2015-02-20T21:48:48.137Z format using new Date().My data is MM/DD/YYYY format so need a way to compare the value i get after subtracting with date in MM/DD/YYYY format. Do i need to change parameter in my filter and input both date and value through ng-model.
http://plnkr.co/edit/unB6m8ZyqGnmiYfeaLoP?p=preview
// Code goes here
var app = angular.module('tempfilter', []);
app.controller('MainCtrl', function($scope) {
$scope.sensordata = [{
name: 'Rob',
"Date": "2015-02-15",
"Temp": 42
}, {
name: 'Bob',
"Date": "2015-02-19",
"Temp": 50
}, {
name: 'Tom',
"Date": new Date().getDate(),
"Temp": 60
}, {
name: 'Sinclair',
"Date": new Date(),
"Temp": 65
}];
$scope.filter = {
value: 2
};
});
app.filter('tempo', function() {
return function(items, field, value) {
var filtered = [];
var d = new Date(); // today!
!d.setDate(d.getDate() - value); //date before today
angular.forEach(items, function(item) {
if (item[field] <= d) {
filtered.push(item);
}
});
return filtered;
};
});
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.19/angular.min.js"></script>
<!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 values lower than {{ filter.value }}</p>
Filtered list:
<ul>
<li ng-repeat="s in sensordata | tempo:'Date':filter.value">{{s.Date|date:'MM/dd/yyyy'}} {{s.name}} {{s.Temp}}
</li>
</ul>
Full List:
<ul>
<li ng-repeat="s in sensordata ">{{s.Date|date}} {{s.name}} {{s.Temp}}
</li>
</ul>
</body>
</html>