Compare and filter data with date in MM/DD/YYYY format from current date using using new Date() while subtracting new Date() from user input value

时光怂恿深爱的人放手 提交于 2019-12-11 09:36:16

问题


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>

回答1:


You can parse a date in mm/dd/yyyy using:

function parseMDY(s) {
  var b = s.split(/\D/);
  return new Date(b[2], b[0]-1, b[1]);
}

Note that IE 8 doesn't support parsing the format in the OP, it is generally considered a bad idea to use the Date constructor or Date.parse for parsing date strings.

Also, a string like 2015-02-15 may be parsed as local time in ES5 compliant browsers, and UTC in those compliant with the ed. 6 draft.

I don't understand you you've used:

!d.setDate(d.getDate() - value);

the ! operator seems redundant.

Edit

From the OP, it seems you're trying to parse dates like Feb 15, 2015, for that you can use:

// Parse MM d, y
function parseMDY(s) {
  var months = {jan:0,feb:1,mar:2,apr:3,may:4,jun:5,
                jul:6,aug:7,sep:8,oct:9,nov:10,dec:11};
  var b = s.split(/[\s,]+/);
  return new Date(b[2], months[b[0].toLowerCase()], b[1]);
}

You can get the current date as new Date(), but that will also have the current time. You can zero the time part, subtract the required period, then compare.

E.g. if you want to filter for date strings more than say 2 days ago:

// Start with the current date and time
var epoch = new Date();

// Set the time to 00:00:00.0
epoch.setHours(0,0,0,0);

// Subtract 2 days
epoch.setDate(epoch.getDate() - 2);

// Compare with string
if (epoch > parseMDY(dateString)) {
  // dateString is more than 2 days before epoch
}



回答2:


Comparison between the current date and date in JSON data works. To compare the JSON date with the current date we could use javascript .getTime() method that returns the number of milliseconds between midnight of January 1, 1970 and the specified date. http://www.w3schools.com/jsref/jsref_getTime.asp

We could use the same thing by using .getTime() for the JSON data and get the required time in milliseconds and compare the two dates. Although the logic works for MM/DD/YYYY adding time offset to the date might be good idea to get the precise date

Can use this link to get the details. I had posted a separate question similar to this query. Can we compare JSON data(MM/DD/YYYY format) in controller with today's date in custom filter and display filtered list in array



来源:https://stackoverflow.com/questions/28639009/compare-and-filter-data-with-date-in-mm-dd-yyyy-format-from-current-date-using-u

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!