我有日期过滤器。我需要在当前日期之前过滤日期列表以前的当前日期和日期列表。
我使用了$scope.date = new Date();
$scope.ago = new Date() < $scope.date;
$scope.before = new Date() > $scope.date;
但不起作用。 这是我的代码:
脚本js:
var app = angular.module("myApp", []);
app.controller("myCtrl", function ($scope,$http) {
$scope.date = new Date();
$http.post("getProject.php")
.success(function (response) {$scope.projects = response;});
var now = new Date().getTime();
$scope.date = new Date(2015, 10, 10);
$scope.ago = now < $scope.date.getTime();
$scope.before = now > $scope.date.getTime();
})
php代码:
<?php
include_once 'dbcon.php';
?>
<!DOCTYPE html>
<html ng-app="myApp">
<head>
<script src="js/angular.min.js" type="text/javascript"></script>
<script type="text/javascript" src="js/app.js"></script>
</head>
<body ng-controller="myCtrl">
<p>Filtering input:</p>
<p><select ng-model="sdate">
<option value="">Search by title</option>
<option value="{{ago}}">Current Deals</option>
<option value="{{before}}">Past Deals</option>
</select></p>
<table>
<thead>
<tr>
<th>Title</th>
<th>Start Date</th>
<th>End Date</th>
<th>Promotion</th>
</tr>
</thead>
<tbody>
<tr ng-repeat="project in projects | filter:sdate | orderBy:'title'">
<td>{{project.title}}</td>
<td>{{project.startdate}}</td>
<td>{{project.enddate}}</td>
<td>{{project.promotion}}</td>
</tr>
</tbody> </table>
</body>
</html>
当前日期将选择时,它将显示当前交易之前的所有日期列表,当选择过去交易时,它将显示前一个当前日期。 我试过AGE的代码,但它不起作用。 我怎么解决这个问题?在此先感谢。
答案 0 :(得分:1)
将比较日期的getTime用作:
function ControllerApp($scope){
var now = new Date().getTime();
$scope.date = new Date(2015, 10, 10);
$scope.ago = now < $scope.date.getTime();
$scope.before = now > $scope.date.getTime();
}
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/angularjs/1.2.23/angular.min.js"></script>
<div ng-app ng-controller="ControllerApp">
Ago: {{ago}}
Before: {{before}}
</div>
&#13;
答案 1 :(得分:0)
我不确定我是否理解你的问题。但是,如果您要查找日期是在今天之前还是在今天之后,您可以使用moment.js轻松实现。 Javascript默认Date对象不是很方便比较。
http://momentjs.com/docs/#/query/is-before/
var now = moment();
var beforeThisMoment = false;
var afterThisMoment = false;
if(now.isBefore(someDate))
{
afterThisMoment = true;
}
else
{
beforeThisMoment = true;
}