gpt4 book ai didi

javascript - JS 日期选择器 : Disable past dates + specific days of the week + specific dates

转载 作者:行者123 更新时间:2023-12-03 02:44:14 31 4
gpt4 key购买 nike

我在 JS 中有一个日期选择器,我禁用了传递的日期并且只允许像这样的星期六:

$(document).ready(function(){
$("#aankomstdatum").datepicker({
dateFormat: "dd-mm-yy",
numberOfMonths:2,
minDate: 0,
beforeShowDay: function(date){
var day = date.getDay();
return [day == 6];
}});
});

我还有一个代码可以让我禁用特定日期,如下所示:

 /** Days to be disabled as an array */
var disableddates = ["26-05-2018"];

function DisableSpecificDates(date) {

var m = date.getMonth();
var d = date.getDate();
var y = date.getFullYear();

// First convert the date in to the mm-dd-yyyy format
// Take note that we will increment the month count by 1
var currentdate = (m + 1) + '-' + d + '-' + y ;

// We will now check if the date belongs to disableddates array
for (var i = 0; i < disableddates.length; i++) {

// Now check if the current date is in disabled dates array.
if ($.inArray(currentdate, disableddates) != -1 ) {
return [false];
}
}
}

添加这个后,它就可以工作了:

beforeShowDay: DisableSpecificDates

我遇到的问题是我无法同时完成这两项工作。我不确定如何禁用过去的日期和除星期六之外的所有日期,并且还禁用数组中的特定给定日期,而它们单独工作。例如,我在尝试时总是遇到语法错误:

    beforeShowDay: DisableSpecificDates, function(date){
var day = date.getDay();
return [day == 6];
}});

这可以做到吗?

最佳答案

是的,这是有可能实现的。您想要创建一个函数并在以下情况下返回 [false]:

  1. 日期是星期六或
  2. date 包含在 disableddates 数组中

示例如下:

var disableddates = ["26-04-2018"];

function DisableDates(date) {
var selectable = !isSaturday(date) && !isDateDisabled(date);

return [selectable];
}

function isSaturday(date) {
var day = date.getDay();

return day === 6;
}

function isDateDisabled(date) {
var m = date.getMonth() + 1;
var d = date.getDate();
var y = date.getFullYear();

// First convert the date in to the dd-mm-yyyy format
if(d < 10) d = '0' + d;
if(m < 10) m = '0' + m;

var currentdate = d + '-' + m + '-' + y;

// Check if disableddates array contains the currentdate
return disableddates.indexOf(currentdate) >= 0;
}

然后只需传递 DisableDates 函数作为 beforeShowDay 选项的值即可:

$("#aankomstdatum").datepicker({
dateFormat: "dd-mm-yy",
numberOfMonths:2,
minDate: 0,
beforeShowDay: DisableDates
});

关于javascript - JS 日期选择器 : Disable past dates + specific days of the week + specific dates,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48167756/

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