FlatPickr - Get the Sundays between a date range for a calendar (inclusive)

Sami A :

I'm using flatPickr (a calendar plugin) to accomplish this. I'm sending the minDate and maxDate, which are both always Sundays, to the JavaScript from a PHP function:

$("#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;
    },
});

In pseudo-code, the logic looks something like this:

// 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"
        }
    ],

Link to docs: https://flatpickr.js.org/examples/#disabling-all-dates-except-select-few

Gabriele Petrioli :

You need to use the .getDay() method of the date, which returns the day of the week (0 is sunday).

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=398773&siteId=1