I am trying to compare today's date we get from new Date() with date in MM/DD/YYYY format and filter the list of data in array with my custom filter. Is there a way to compare this..I have tried to set the hours to (0,0,0,0) and compare but it does not work..can we set hours in json data too and compare ...My javascript looks somthing like this..and here is the plunker link. http://plnkr.co/edit/unB6m8ZyqGnmiYfeaLoP?p=preview. I can compare id:4,5 but not 1,2,3 because of different date format...The main purpose was to display the data for the last few days based on user input..Is there a way to compare this two dates and filter the required data.
// Code goes here
var app = angular.module('tempfilter', []);
app.controller('MainCtrl', function($scope) {
$scope.sensordata = [
{id:'id:1',name:'Rob',Date:"5/11/2014","Temp":42},
{id:'id:1',name:'Don',Date:"2015-05-11","Temp":42},
{id:'id:2',name:'Bob',Date:"2015-02-22T17:16:14.720Z","Temp":50},
{id:'id:3',name:'Tom', Date: new Date().getDate(),"Temp":60},
{id:'id:4',name:'Sinclair',Date: new Date(),"Temp":65}
];
$scope.filter = { value:16490 };
});
app.filter('tempo', function() {
return function( items, field, value) {
var filtered = [];
var epoch = new Date(); // today!
epoch.setHours(0,0,0,0);
epoch.setDate(epoch.getDate()-value);
angular.forEach(items, function(item) {
if (item[field]>epoch){
filtered.push(item);
}
});
return filtered;
};
});