flatpickr / flatpickr

lightweight, powerful javascript datetimepicker with no dependencies

Home Page:https://flatpickr.js.org

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

minDate does not respect blocked weekends

Jessman5 opened this issue · comments

when blocking weekends like this:

'disable': [
    function(date) {
        // blocking Sunday (0) and Saturday (6)
        return (date.getDay() === 0 || date.getDay() === 6);
    }
],

and setting up a minDate of 4 days like that (today and the following 3 days blocked:

// blocking today + 3 following days
minDate: new Date().fp_incr(4)

the date picker does not behave as expected.

Expectation: Weekends are blocked.

When on a Wednesday selecting a date, the first option should be Tuesday since today and the following 3 days (Wed–Mon, excl. weekends) are blocked.

Actual behaviour: Weekends are not blocked for minDate.

When on a Wednesday selecting a date, the first option is Sunday because the weekend is included in the count. But then the Weekend blocking steps in and actually blocks the weekend from being picked (as expected) so the first possible day is the following Monday.

It doesn't change anything if the minDate option comes before or after the disable one.

with weekends blocked:
Bildschirmfoto 2023-09-20 um 15 17 46

without weekends blocked:
Bildschirmfoto 2023-09-20 um 15 25 37

Your Environment

  • flatpickr version used: 4.6.13
  • Browser name and version: Chrome 116.0.5845.187
  • OS and version: Mac OS Monterey 12.6.8

Any recommendations are appreciated. I use this with plain Javascript and Blazor.
Best would be to have the minDate option recognize and respect the disable option.

Thank you.

I went over to Stackoverflow to get some help since it seems to be quite empty here. I got some valuable hints.

Thats what I got working:

  1. I set the default lead time value in a variable to 4
  2. I convert the date + leadtime to a string which gives me Sun Oct 01 2023 00:00:00 GMT+0200
  3. I then check if the String contains "Sat" or "Sun"
  4. I then initialise the flatpickr with the correct minDate option. (+2 for the weekend count)
var leadtime = 4;
var leattimeString = new Date().fp_incr(leadtime).toString();

if (leattimeString.includes('Sat') || leattimeString.includes('Sun')) {
    flatpickr('.js-flatpickr', {
        ...
        // when leadtime lands on a Saturday or Sunday add 2 extra days to count the weekend
        minDate: new Date().fp_incr(6),
    }
}
Bildschirmfoto 2023-09-27 um 16 27 27

Sounds dirty, looks dirty, but does what I want.

It doesn't seem to work to initialise flatpickr first and then have the correct lead time inserted afterwards.
It also doesn't work to use minDate: function {..}