I'm struggling to compare today's date obtained from new Date() with a date in MM/DD/YYYY format and then filter the array data using my own custom filter. Is there a way to achieve this? I attempted to set the hours to (0,0,0,0) and compare them but it didn't work as expected. Can we also set hours in JSON data and perform a comparison? Here is a snippet of my JavaScript code, and you can check out the Plunker link here. I can successfully compare id:4 and 5, but struggle with 1, 2, and 3 due to different date formats. My goal was to display data for the last few days based on user input. Any suggestions on how to compare these 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;
};
});