AngularJS - 检查是否选择的日期等于下一个营业日 [英] AngularJS - Check if selected date is equal to next business date
问题描述
我想,以检查是否选定日期等于下一个营业日的AngularJS。如果选定的日期不等于下一个营业日,将发出警告显示,像Twitter的引导的警报预警,采用NG-显示angularjs指令。
I am trying to check if selected date is equal to next business date using AngularJS. If selected date is not equal to the next business date, a warning will be shown, like twitter bootstrap's alert-warning, using ng-show angularjs directive.
下面是当前 小提琴 此
Here's the current Fiddle for this.
目前,在第二个工作日由getNextBusinessDay()返回$范围的功能。比较被NG-改变触发=comparisonResult(),它应该返回true /基于比较假,但不工作。
Currently, the next business day is returned by getNextBusinessDay() $scope function. The comparison is triggered by ng-changed="comparisonResult()" which should return true/false based on comparison, but isn't working.
在这里,我的html code:
<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 code:
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;
}
};
});
任何帮助或建议将AP preciated。提前致谢。 :)
Any help or suggestion will be appreciated. Thanks in advance. :)
推荐答案
在比较日期时,使用 a.getTime()== b.getTime()
而不是 A == b
。
When comparing dates, use a.getTime() == b.getTime()
instead of a == b
.
这篇关于AngularJS - 检查是否选择的日期等于下一个营业日的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!