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

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

问题描述

尝试将 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

我可以使用 new 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

我们可以通过对 JSON 数据使用 .getTime() 来使用相同的东西,并以毫秒为单位获取所需的时间并比较两个日期.虽然逻辑适用于 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

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

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