You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何修改jQuery Datepicker的beforeShowDay,允许选择周日至周四?

Solution for jQuery Datepicker to Allow Sunday-Thursday Selection

Sure thing! You can absolutely customize the beforeShowDay callback to allow only Sunday through Thursday instead of the default Monday-Friday that $.datepicker.noWeekends enforces. Here's a straightforward solution:

The core idea is to replace $.datepicker.noWeekends with a custom function that checks the day of the week. Remember: JavaScript's Date.getDay() returns 0 for Sunday, 1 for Monday, up to 6 for Saturday. We want to enable days where the value is 0 (Sun) to 4 (Thu), and disable Friday (5) and Saturday (6).

Here's your modified code with the custom validation integrated:

$(function () {
  // Custom function to allow Sunday through Thursday
  const allowSunToThu = function(date) {
    const dayOfWeek = date.getDay();
    // Return [true, ...] if day is 0-4 (Sun-Thu), else [false, ...]
    return [(dayOfWeek >= 0 && dayOfWeek <= 4), ''];
  };

  $("#datepicker").datepicker({
    onSelect: function () {
      $("#sched_macs_button").show(300);
    },
    dateFormat: 'yy-mm-dd',
    changeMonth: true,
    beforeShowDay: allowSunToThu, // Use our custom validation here
    numberOfMonths: 1,
    minDate: dateToday
  });
});

A quick breakdown of how this works:

  • The allowSunToThu function takes a date object as input.
  • We grab the day of the week using date.getDay().
  • We return an array where:
    • The first element is true if the day is allowed (making it selectable), or false if it should be disabled (greyed out and unclickable).
    • The second element is an optional CSS class (we left it empty here, but you could add a class name if you want to style allowed/disabled dates differently).

This approach is clean, uses Datepicker's built-in callback system as intended, and is easy to adjust if you ever need to change the allowed days later.

内容的提问来源于stack exchange,提问作者spas2k

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 06:36:48