2

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;
    };
});
Don
  • 305
  • 4
  • 17
  • 1
    I don't know if this answers your question, but for dates I use moment.js. It makes date formats, operations and comparisons easier. – jmbmage Feb 23 '15 at 20:02
  • Looking to do this without using moment.js..I think it should be possible.Hope somebody has the answer. – Don Feb 23 '15 at 20:14

2 Answers2

2

I think you can use getTime() method of date. For Example: var today = new Date().getTime() var jsonDate = new Date("2015-02-20").getTime() and then compare today and jsonDate as per yor need. getTime() functions returns time in milliseconds since 1970/01/01. I hope this will help you.

Anand Gargate
  • 479
  • 5
  • 16
  • Your logic works just changed the date format for the json data to have time offset and comparision worked perfectly – Don Feb 24 '15 at 22:40
  • Do you know how we could filter the json date object. while trying to filter the json date object instead of string. http://plnkr.co/edit/KZdNULEXCQefCTUR0PML?p=preview – Don Apr 09 '15 at 03:50
  • http://stackoverflow.com/questions/29591051/can-we-filter-nested-json-data-using-custom-filter-in-angular-js – Don Apr 12 '15 at 15:29
1

side note: I don t know angular.js.

As Anand said, did you tried to Date.getTime()? new Date() can generate a Date object for all your examples, with getTime() you can get the number of milliseconds, so you can compare far more easily.

You may want to take getTime at the creation of your JSON, so that you don t need to calculate it in the loop everytime.

Should look as:

// 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);

      //HERE
      epoch = epoch.getTime();
      
      angular.forEach(items, function(item) {
        //HERE
        if (new Date(item[field]).getTime() > epoch){
          filtered.push(item);
        }
      });
      return filtered;
    };
});
DrakaSAN
  • 7,673
  • 7
  • 52
  • 94
  • Thanks a lot just changed the time format for the json data to have the time offset too for the comparison and it worked.The MM/DD/YYY format works however for YYYY-MM-DD it requires the time offset, else it displays incorrectly.For example in id:4 it displays data as 02/22/2015 instead of 02/23/2015 although the comparison works..http://plnkr.co/edit/5IhJYSXvqa5nwd87y8kD?p=preview. Adding time offset to you logic solves everything. – Don Feb 24 '15 at 23:43