question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Ability to set minimum length of range selection

See original GitHub issue

I can’t figure out a way to enforce a minimum length for a range flatpickr.

E.g. when selecting dates for a hotel booking, I don’t want the user to be able to chose the same date for both check-in and check-out. The check-out date must be at least 1 day later than check-in.

I tried using set() to change minDate inside an onChange() callback when the user chooses the first date, but this resulted in an infinite loop (i’m using vue-bulma-datepicker).

Your Environment

  • flatpickr version used: 2.4.2
  • Browser name and version: Chrome 57
  • OS and version: OSX 10.12.3

Issue Analytics

  • State:closed
  • Created 7 years ago
  • Comments:6 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
johnobonocommented, Sep 16, 2020

I solved it using following code:

onChange (selectedDates, value, datepicker) {
    if (selectedDates.length === 2) {
        let diffTime = Math.abs(selectedDates[1] - selectedDates[0]);
        let diffDays = Math.ceil(diffTime / (1000 * 60 * 60 * 24)); 
        let addDays = Math.max(0, <YOUR_RANGE_MIN_LENGTH> - diffDays);

        if (addDays > 0) {
            selectedDates[1].setDate(selectedDates[1].getDate() + addDays);
            datepicker.setDate(selectedDates);
            setTimeout(() => {
                datepicker.jumpToDate(selectedDates[0]);
                datepicker.open();
            });
        }
    }
}

replace <YOUR_RANGE_MIN_LENGTH> with the desired range length

0reactions
xcrapcommented, Mar 25, 2020

Thanks for the quick reply. I tried switching to vue-datepicker which uses the latest version of flatpickr (v2.4.7), but I still have the same problem.

Here’s what I’m trying to do, I’m not sure if this is even the right approach:

onChange (selectedDates, value, datepicker) {
  if (selectedDates.length === 1) {
    datepicker.set('minDate', addDays(selectedDates[0], 1)) // addDays() is from date-fns
  } else {
    datepicker.set('minDate', 'today') // reset back to default
  }
}

It every time I call datepicker.set(), onChange() is called again causing the loop.

But even without the loop (if I comment out the datepicker.set('minDate', 'today') line), flatpickr doesn’t seem to like setting minDate to a date after the first selected date.

I feel like there must be a better way to enforce minimum selection length…

hi @adambiggs, I’m trying to implement the same solution as you, did you manage to solve the problem with the set minDate on the range plugin ?

Read more comments on GitHub >

github_iconTop Results From Across the Web

Apply data validation to cells - Microsoft Support
In the Data box, select the type of restriction that you want. For example, to set upper and lower limits, select between. Enter...
Read more >
Set capacity limits on your Auto Scaling group - Amazon EC2 ...
Capacity limits represent the minimum and maximum group size that you want for your Auto Scaling group. You set limits separately for the...
Read more >
Part 15 - Contracting by Negotiation | Acquisition.GOV
At the conclusion of discussions, each offeror still in the competitive range shall be given an opportunity to submit a final proposal revision....
Read more >
Computerized Adaptive Testing (CAT) - NCSBN
The minimum number of items for both the RN and PN exam is 75 rather than 75 and 85, respectively. The maximum number...
Read more >
How to use MIN function in Excel - Ablebits
The MIN function checks your data range and returns the smallest value in the set. Its syntax is the following: MIN(number1, [number2], …)....
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found