使用新的Date()从当前日期比较并过滤具有MM/DD/YYYY格式的日期的数据,同时从用户输入值中减去新的Date() [英] 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

查看:353
本文介绍了使用新的Date()从当前日期比较并过滤具有MM/DD/YYYY格式的日期的数据,同时从用户输入值中减去新的Date()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

尝试将2015-02-21T21:48:48.137Z格式的新日期(今天的日期)与MM/DD/YYYY格式的日期进行比较,这是我的json数据.我想根据用户输入的天数(根据用户输入的天数来获取数据)来过滤今天前天的所有数据.我可以获取天数并将其从今天的日期中减去.不知道如何将此日期与MM/DD/YYYY格式的Json日期值进行比较.我应该解析Date()以便与MM/DD/YYYY格式的JSON数据进行比较

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

我能够接受用户输入并使用新的Date()从今天的日期中减去基于2015-02-20T21:48:48.137Z格式的日期.我的数据是MM/DD/YYYY格式,因此需要一种比较方式我用MM/DD/YYYY格式加上日期减去后得到的值.我是否需要更改过滤器中的参数,并通过ng-model输入日期和值.

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>

推荐答案

将当前日期与JSON数据中的日期进行比较.要将JSON日期与当前日期进行比较,我们可以使用javascript .getTime()方法,该方法返回1970年1月1日午夜到指定日期之间的毫秒数. http://www.w3schools.com/jsref/jsref_getTime.asp

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

我们可以通过将.getTime()用于JSON数据来使用相同的东西,并获取所需的时间(以毫秒为单位)并比较两个日期.尽管该逻辑适用于MM/DD/YYYY,但为日期添加时间偏移可能是获取准确日期的好主意

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

可以使用此链接获取详细信息.我已经发布了一个与此查询类似的单独问题. 我们可以在自定义过滤器中将控制器中的JSON数据(MM/DD/YYYY格式)与今天的日期进行比较,并在数组中显示过滤后的列表

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

这篇关于使用新的Date()从当前日期比较并过滤具有MM/DD/YYYY格式的日期的数据,同时从用户输入值中减去新的Date()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆