FlatPickr-获取日历日期范围之间的星期日(包括日历)

时间:2020-04-03 14:24:25

标签: javascript jquery

我正在使用flatPickr(日历插件)来完成此操作。我正在从PHP函数将minDate和maxDate都始终是星期日发送到JavaScript:

$("#weeklySelector").flatpickr(
{
    inline: true,
    enableTime: false,
    dateFormat: "Y-m-d",
    minDate: "<?php echo getSecondSunday($oldestDay[0], $newestDay[0]); ?>",
    maxDate: "<?php echo getLastSunday($newestDay[0], getSecondSunday($oldestDay[0], $newestDay[0])); ?>",
    defaultDate: "<?php echo getLastSunday($newestDay[0], getSecondSunday($oldestDay[0], $newestDay[0])); ?>",
    enable: [
        function(date) {
            // Enable only the Sundays between the minDate and maxDate
            // Include the minDate & maxDate because they both always will be Sundays

        }
    ],
    onChange: function(selectedDates, dateStr, instance) {
        weeklyDate = dateStr;
    },
});

在伪代码中,逻辑看起来像这样:

// minDate = "2020-04-05";
// maxDate = "2020-04-26";
    enable: [
        function(date) {
            minDate, while(minDate+7 <= maxDate);
            // Output: "2020-04-05", "2020-04-12", "2020-04-19", "2020-04-26"
        }
    ],

链接到文档:https://flatpickr.js.org/examples/#disabling-all-dates-except-select-few

1 个答案:

答案 0 :(得分:1)

您需要使用日期的.getDay()方法,该方法返回星期几( 0是星期日)。

    enable:[ 
        function(date) {
            return date.getDay() === 0; // 0 is sunday
        }
    ]