Angularjs比较日期

时间:2016-06-02 08:36:02

标签: angularjs date

我正在尝试比较2个日期并使用结果过滤我的数据:

我的控制器:

  $scope.checkDate = function(startDate){
    var todaysDate = new Date();
    return function(coupon){
        return coupon[startDate]<= todaysDate;
    };
}

我的HTML

  <div class="col-sm-3" id="coupon-tiles" ng-repeat="coupon in allCoupons| filter: checkDate('startDate')" ng-if="coupon.amount>0" >
        <div>
            <image ng-src={{coupon.image}}>
        </div>
        <div>{{coupon.title}}</div>
        <div>price:{{coupon.price}}</div>
        <div>start date:{{coupon.startDate}}</div>
        <div>end date:{{coupon.endDate}}</div>

        <div class="btn-group" role="group" aria-label="...">
            <button type="button" class="btn btn-default" ng-    click="buy(coupon)">Purchase</button>
        </div>

    </div>

优惠券通过json从db到达,日期是java sql日期,因此格式为yyyy,mm,dd,Jersey和Jackson进行解析。

似乎没有任何作品,有人可以提供帮助

3 个答案:

答案 0 :(得分:1)

您的数据库日期是字符串格式。您需要将其转换为JS Date对象以进行比较。请尝试以下方法:

$scope.checkDate = function(startDate){
var todaysDate = new Date();
 return function(coupon){
    return Date.parse(coupon[startDate]) <= todaysDate;
 };
}

答案 1 :(得分:0)

尝试简化过滤器功能,如

$scope.checkDate = function(startDate){
    return startDate.getTime() <= new Date().getTime();
}

在你的HTML中

 <div class="col-sm-3" id="coupon-tiles" ng-repeat="coupon in allCoupons| filter: checkDate(coupon.startDate)" ng-if="coupon.amount>0" >

答案 2 :(得分:0)

我正在尝试这个,这是有效的:

$callPlans = CustomerCallPlan::whereNotNull('id')->get();

foreach ($callPlans as $callPlan) {
    $callPlan->numbertemplate = (whetever you need);
    $callPlan->save(); //save the changes
}

要使用var startDate = $filter('date')($scope.StartDate, "yyyy-MM-dd"); var endDate = $filter('date')($scope.EndDate, "yyyy-MM-dd"); if (startDate > endDate) { //some code } ,您需要在控制器中注入$filter

相关问题