角度过滤器,将JSON文件日期与今天的日期进行比较

时间:2016-06-28 18:32:31

标签: javascript angularjs json date

<p id="appt_time" ng-if="x.dateTime | date: MM/dd/yyyy == {{todaysDate}}">SHOW ME IF TRUE{{todaysDate}} </p>

Plunkr:https://plnkr.co/edit/r2qtcU3vaYIq04c5TVKG?p=preview

x.dateTime | date: MM/dd/yyyy根据我的json文件获取过滤后的日期和时间:06/18/2016

我想要完成的是将此“x.dateTime”与今天的日期进行比较,并在声明为真时显示该段落。所以在我的角度文件中,我有$scope.todaysDate = "06/18/2016",但段落没有显示。

HTML文件:

<!DOCTYPE html>
<html>

  <head>
    <link rel="stylesheet" href="style.css">
    <script src="https://code.angularjs.org/1.5.5/angular.js"></script>
    <script src="script.js"></script>

  </head>

  <body ng-app="myApp" ng-controller="myCtrl">
<div ng-repeat="x in information">

  <!--Compare Json Object to Javascript Object-->
  <p ng-if ="x.dateTime">{{x.dateTime | date: MM/dd/yyyy }}</p> 
  <!--Compare Json Object to Today's Date-->
<p id="appt_time" ng-if="x.dateTime.valueOf() === todaysDate.valueOf()">Open in: {{todaysDate}} </p>
</div>
  </body>

</html>

JS文件:

// declare a module
var myAppModule = angular.module('myApp', []);
  var todaysDate = new Date();


// configure the module.
// in this example we will create a greeting filter
myAppModule.controller('myCtrl', ['$scope','$http', function($scope, $http) 
{

    $scope.todaysDate = todaysDate;
    $scope.test = "Volvo";

    $http.get("time.json")
    .then(function(data) {
        $scope.information = data;
    });
}]
);

JSOn文件:

{
  "dateTime":"2016-06-18T18:41:00.748-04:00"

}

3 个答案:

答案 0 :(得分:4)

因为你正在处理日期和时间,所以事情可能会很快变得棘手。我建议使用Moment.js library,因为它使这非常简单:

"day"

ng-if="x.DateTime.substring(0, 10) === todaysDate" 表示您想要比较这两个值的粒度。但是,即使没有该库,它也相对容易:

$scope.todaysDate

您将"2016-06-18"更改为# ... for msgFile in msgFiles: count = count + 1 msg = outlook.OpenSharedItem(path + msgFile) date = str(msg.SentOn) del msg # ...

答案 1 :(得分:0)

如果日期是今天,您可以添加validate函数,该函数将返回true。不要忘记注入$filter

$scope.validate = function(date) {
   return $filter('date')(date. 'your format') === $filter('date')(new Date(). 'your format');
}

在html中

<p id="appt_time" ng-if="validate(x.dateTime)">SHOW ME IF TRUE{{x.dateTime}} </p>

答案 2 :(得分:0)

请参阅foll链接:

https://plnkr.co/edit/ZLJB5i1nQu9RYUV5FAX3?p=preview

// time.json

[{
  "dateTime":"2016-06-18T18:41:00.748-04:00"

}]

//的script.js

 $http.get("time.json")
    .then(function(response) {
        $scope.information = response.data;
        debugger
    });

您的问题已解决