我们能否将JSON数据(MM/DD/YYYY格式)与当前自定义过滤器中的日期进行比较,并在数组中显示筛选过的列表?

时间:2023-01-14 14:33:29

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.

我正在尝试将我们从new date()获得的今天的日期与MM/DD/YYYY格式的日期进行比较,并用我的自定义过滤器过滤数组中的数据列表。有办法比较一下吗我试着把时间设置为(0,0,0,0,0)并进行比较,但是不管用。我们是否也可以在json数据中设置小时并进行比较……我的javascript看起来是这样的。这是柱塞连杆。http://plnkr.co/edit/unB6m8ZyqGnmiYfeaLoP?p=preview。我可以比较id:4,5,但不是1,2,3,因为日期格式不同……主要目的是基于用户输入显示最近几天的数据。是否有一种方法可以比较这两个日期并过滤所需的数据。

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

2 个解决方案

#1


1  

side note: I don t know angular.js.

附注:我不知道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.

就像Anand说的,你试过约会了吗?new Date()可以为您的所有示例生成一个Date对象,通过getTime()您可以获得毫秒数,以便更容易地进行比较。

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.

您可能希望在创建JSON时使用getTime,这样就不需要每次都在循环中计算它。

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

#2


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.

我认为您可以使用getTime()方法。例如:var today = new Date(). gettime () var jsonDate = new Date(“2015-02-20”). gettime(),然后根据您的需要比较今天和jsonDate。getTime()函数的返回时间为1970/01/01。我希望这能对你有所帮助。

#1


1  

side note: I don t know angular.js.

附注:我不知道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.

就像Anand说的,你试过约会了吗?new Date()可以为您的所有示例生成一个Date对象,通过getTime()您可以获得毫秒数,以便更容易地进行比较。

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.

您可能希望在创建JSON时使用getTime,这样就不需要每次都在循环中计算它。

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

#2


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.

我认为您可以使用getTime()方法。例如:var today = new Date(). gettime () var jsonDate = new Date(“2015-02-20”). gettime(),然后根据您的需要比较今天和jsonDate。getTime()函数的返回时间为1970/01/01。我希望这能对你有所帮助。