gpt4 book ai didi

javascript - JQuery 日期选择器 : Disable dates explicitly enabled

转载 作者:行者123 更新时间:2023-12-02 19:23:03 28 4
gpt4 key购买 nike

我有一个日期选择器,可以过滤可选择的日期。要求只有 T、W、Th 和每月一个随机的星期六。到目前为止,我已经能够使用下面的代码来完成此任务。现在,我的客户希望能够出于各种原因(例如假期、办公室关闭等)在 T、W、Th 的某些日子进行屏蔽。我不知道如何完成这个新要求。有什么想法吗?

var SaturdayDate = ["7-25-2020", "8-15-2020" ];

$(function() {

$("#my_date_picker").datepicker({
beforeShowDay: function(dt) {
return [dt.getDay() == 2 || dt.getDay() == 3 || dt.getDay() == 4 || enableThisDay(dt), ""];
},
minDate: 1
});
});

function enableThisDay(date) {
var oDate = $.datepicker.formatDate('m-d-yy', date);
if ($.inArray(oDate, SaturdayDate) != -1) {
return [true];
}
}

最佳答案

我会保留任何数组以使用 Date 对象而不是字符串,从而消除潜在的格式问题。我有两个数组:“随机”星期六和假期。然后我有一个函数来检查日期是否启用。

// Dates in JS use 0 indexed months, so 6 is July and 7 is August
var saturdays = [ new Date(2020, 6, 25), new Date(2020, 7, 15) ];
var holidays = [ new Date(2020, 6, 22), new Date(2020, 6, 2), new Date(2020, 7, 13) ];

$(function() {
$("#my_date_picker").datepicker({
beforeShowDay: enableThisDay,
minDate: 1
});
});

function enableThisDay(date) {
var enabled = false;
// if it's Tue, Wed, Thu, enable it
if ([2,3,4].includes(date.getDay())) {
enabled = true;
}
// if it's a holiday, disable it
if (holidays.some(h => h.valueOf() === date.valueOf())) {
enabled = false;
}
// if it's a saturday, disable it
// note that if it's a saturday, that overrides holiday
// if holidays override saturday, swap this statement with
// the one immediately above
if (saturdays.some(s => s.valueOf() === date.valueOf())) {
enabled = true;
}
return [enabled, ""];
}
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.min.js"></script>
<link href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css" rel="stylehseet">
<link href="https://code.jquery.com/ui/1.12.1/themes/ui-lightness/jquery-ui.css" rel="stylehseet">
<input type="text" id="my_date_picker">

关于javascript - JQuery 日期选择器 : Disable dates explicitly enabled,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62777503/

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