gpt4 book ai didi

javascript - 根据条件启用日期

转载 作者:行者123 更新时间:2023-12-03 04:17:17 25 4
gpt4 key购买 nike

我正在开发 angularjs 应用程序。我有两个日期字段,要求是当用户在“可用日期”日历中选择日期时,“到期日期”字段应启用,并且用户应该能够选择所选日期后 24 天内的日期在可用日期字段中。例如,当用户在可用日期字段中选择日期为 2017-03-02 时,应启用过期日期字段,并且日历应仅启用 2017-03-02 后的 24 天(即 2017-03-25)以及所有日期应禁用,以便用户无法选择 2017-03-25 之后或 2017-03-02 之前的任何日期。请查看演示here

js代码:

myApp.controller("myController", ["$scope",
function($scope) {
var today = new Date();
$scope.AvailableDate = new Date();
$scope.ExpireDate = new Date();
$scope.dateFormat = 'yyyy-MM-dd';
$scope.availableDateOptions = {
formatYear: 'yy',
startingDay: 1,
minDate: "2016-03-12",
maxDate: today
};
$scope.expireDateOptions = {
formatYear: 'yy',
startingDay: 1,
minDate: today,
maxDate: "2017-06-12"
};
$scope.availableDatePopup = {
opened: false
};
$scope.expireDatePopup = {
opened: false
};
$scope.ChangeExpiryMinDate = function(availableDate) {
if (availableDate != null) {
var expiryMinDate = new Date(availableDate);
$scope.expireDateOptions.minDate = expiryMinDate;
$scope.ExpireDate = expiryMinDate;
//code to set maxDates in Expire Date field -start
var date = new Date(expiryMinDate);
var newdate = new Date(date);
newdate.setDate(newdate.getDate() + 3);
var dd = newdate.getDate();
var mm = newdate.getMonth() + 1;
var y = newdate.getFullYear();
var someFormattedDate = y + '/' + dd + '/' + mm;
$scope.expireDateOptions.maxDate = someFormattedDate;
//code to set maxDates in Expire Date field -end
}
};
$scope.ChangeExpiryMinDate();
$scope.OpenAvailableDate = function() {
$scope.availableDatePopup.opened = !$scope.availableDatePopup.opened;
};
$scope.OpenExpireDate = function() {
$scope.expireDatePopup.opened = !$scope.expireDatePopup.opened;
};
}
]);

下面是我尝试过但没有成功的代码。

 var date = new Date(expiryMinDate);
var newdate = new Date(date);
newdate.setDate(newdate.getDate() + 3);
var dd = newdate.getDate();
var mm = newdate.getMonth() + 1;
var y = newdate.getFullYear();
var someFormattedDate = y + '/' + dd + '/' + mm;
$scope.expireDateOptions.maxDate = someFormattedDate;

最佳答案

根据您的要求,我使用以下方法来保留 minDate 和 maxDate 设置:

$scope.ChangeExpiryMinDate = function(availableDate) {
if (availableDate != null) {
var availableDate = new Date(availableDate);
var expiryMinDate = angular.copy(availableDate);
expiryMinDate.setDate(expiryMinDate.getDate() + 23);

$scope.ExpireDate = availableDate;
$scope.expireDateOptions.minDate = availableDate;
$scope.expireDateOptions.maxDate = expiryMinDate;
} else {
delete $scope.ExpireDate;
}
};

$scope.ChangeExpiryMinDate($scope.AvailableDate);

您忘记将当前的可用日期传递到函数中,因此当 Controller 首次加载时,到期日期字段已经受到限制。

要禁用周末,您可以更新 ExpiryDate 选项:

$scope.expireDateOptions = {
formatYear: 'yy',
startingDay: 1,
minDate: today,
maxDate: "2017-06-12",
dateDisabled: function(data) {
var date = data.date;
var mode = data.mode;
return (mode === 'day' && ( date.getDay() === 0 || date.getDay() === 6 ));
}
};

我在这里创建了一个工作 JSFiddle:https://jsfiddle.net/1ahyk735/

关于javascript - 根据条件启用日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44092086/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com