简体   繁体   English

按日期的角度过滤数据

[英]Angular filter data by date

I have a json as follows which i am receiving using an angular service: 我有一个JSON,如下所示,我正在使用角度服务接收:

[
    {
        "id": 2,
        "order_status": "R",
        "order_date": "2015-09-12T07:58:24.733834Z",
        "update_timestamp": "2015-10-05T04:22:44.904227Z"
    },
    {
        "id": 8,
        "order_status": "D",
        "order_date": "2015-09-18T03:46:50.469051Z",
        "update_timestamp": "2015-09-23T19:19:31.658001Z"
    }
]

I am receiving this data into a variable $scope.order_items and i am filtering them by order_status column: 我将这些数据接收到变量$scope.order_itemsorder_status列对其进行过滤:

$scope.order_items = data;
$scope.received = ($scope.order_items.filter(function(item) { return (item.order_status == 'R');}));
$scope.delivered = ($scope.order_items.filter(function(item) { return (item.order_status == 'D');}));

Now i am trying to filter the data by the column order_date such that i get all today's data entries into a separate variable. 现在,我尝试按order_date列过滤数据,以便将今天的所有数据输入到单独的变量中。 How do i achieve this? 我该如何实现?

EDIT : Just realized that you don't actually have a Date() object for the order_date so you'll have to create one before doing the == check belo. 编辑 :刚刚意识到您实际上没有order_date的Date()对象,因此您必须在进行== check belo之前创建一个对象。

There are a number of good Javascript Date manipulation libraries available(rather than doing the setHours(...) trick below, but something like this is what you want. 有很多好的Javascript Date操作库可用(而不是下面的setHours(...)技巧),但是您需要这样的东西。

       var Date today = new Date();
       var todaysDates = $filter('filter')($scope.order_items, function(item) {

          if(item.order_date.setHours(0,0,0,0) == today.setHours(0,0,0,0)) {
              return true;
          } else {
              return false;
          }

        });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM