3

我想檢查選定日期是否等於使用AngularJS的下一個生意日期。如果選擇的日期不等於下一個生意日期,則會顯示一條警告,如twitter bootstrap的警報警告,使用ng-show angularjs指令。AngularJS - 檢查選定的日期是否等於下一個生意日期

這是當前的Fiddle

當前,下一個工作日由getNextBusinessDay()$ scope函數返回。比較是由ng-changed =「comparisonResult()」觸發的,它應該根據比較返回true/false,但不起作用。

這裏我的html代碼:

<body ng-app="angularjs-starter" ng-controller="MainCtrl"> 
<div class="container"> 
    <section id="datepicker"> 
    <div class="bs-docs-example"> 
     <form class="form-horizontal well"> 
     <div class="control-group input-append"> 
      <label for="inputDatepicker" class="label" style="margin-right:6px;"> 
      Selected Date</label> 
      <input id="inputDatepicker" class="input-small" type="text" 
      ng-model="selectedDate" ng-changed="comparisonResult()" 
      data-date-format="dd/mm/yyyy" bs-datepicker> 
      <button type="button" class="btn" data-toggle="datepicker"> 
      <i class="icon-calendar"></i></button> 
     </div> 
     </form> 
    </div> 
    </section> 
</div> 
</body> 

這裏是我的js代碼:

var app = angular.module('angularjs-starter', ['$strap.directives']); 

app.controller('MainCtrl', function($scope, $window, $location) { 

    // Datepicker directive 
    $scope.selectedDate = null; 

    $scope.getNextBusinessDay = function() { 
     return $scope.getDeliveryDateObj(1); 
    } 

    $scope.getDeliveryDateObj = function(businessDaysLeftForDelivery){ 
     var now = new Date(); 
     var dayOfTheWeek = now.getDay(); 
     var calendarDays = businessDaysLeftForDelivery; 
     var deliveryDay = dayOfTheWeek + businessDaysLeftForDelivery; 
     if (deliveryDay >= 6) { 
      businessDaysLeftForDelivery -= 6 - dayOfTheWeek; // deduct this-week days 
      calendarDays += 2; // count this coming weekend 
      deliveryWeeks = Math.floor(businessDaysLeftForDelivery/5); // how many whole weeks? 
      calendarDays += deliveryWeeks * 2; // two days per weekend per week 
     } 
     now.setTime(now.getTime() + calendarDays * 24 * 60 * 60 * 1000); 

     now.setUTCHours(0); 
     now.setUTCMinutes(0); 
     now.setUTCSeconds(0); 
     now.setUTCMilliseconds(0); 

     return now; 
    } 

    $scope.getNextBusinessDay(); 

    $scope.comparisonResult = function() { 
     if($scope.selectedDate == $scope.getNextBusinessDay()) { 
      return false; 
     } else { 
      return true; 
     } 
    }; 

}); 

任何幫助或建議將不勝感激。提前致謝。 :)

回答

3

比較日期時,請使用a.getTime() == b.getTime()而不是a == b

+0

非常感謝。這只是解決了這裏的錯誤。 – sharic19

0

現在是2017年,如果您使用Angular> = v1.2,您可以使用angular.equals(date1,date2)

相關問題