2

Could someone guide me to (if possible) how to prevent the user from booking certain dates?

I manage to prevent clicking on the previous dates and the end date of the contract but not the dates of unavailability. enter image description here

In my database I store dates (start date, end date) for employees.

How can I prevent the user from clicking dates when he wants to book a session with one of my employees?

Thank you!

5
  • Take a look at the API documentation, especially the max/min inputs. Commented Aug 13, 2018 at 10:41
  • Hello, I already use [min] and [max] for the duration of the contract of my employees, but I do not know how to add also the dates of unavailability (leave, illness) :-/ Commented Aug 13, 2018 at 10:50
  • I don't think you should use a single date picker for every case : use one picker by case (one for leave/illness, one for vacations, one for work ...) Commented Aug 13, 2018 at 10:51
  • The problem is that to make a reservation (for the client), I need only one calendar :-/ Commented Aug 13, 2018 at 10:55
  • Then use a validator to state that the date is not available, making the form invalid Commented Aug 13, 2018 at 10:56

2 Answers 2

2

Use [matDatepickerFilter]="myFilter"

DEMO

myFilter = (d: Date): boolean => {

        let date: any;
        let month: any;

        if (d.getDate().toString().length < 2) {
            date = '0' + d.getDate().toString()
        } else {
            date = d.getDate().toString()
        }

        if ((d.getMonth() + 1).toString().length < 2) {
            month = '0' + (d.getMonth() + 1).toString()
        } else {
            month = (d.getMonth() + 1).toString()
        }

        const day = d.getFullYear().toString() + "-" + month + "-" + date;

        return !this.disableDateList.includes(day);
    }

HTML:

<mat-form-field class="col-md-6">
     <input matInput [matDatepickerFilter]="myFilter" [matDatepicker]="picker" formControlName="date"
                        placeholder="Choose date">
          <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
          <mat-datepicker picker touchUi="true"></mat-datepicker>
  </mat-form-field>
Sign up to request clarification or add additional context in comments.

5 Comments

Thank you @UnluckyAj, this is exactly what I was looking for !
please accept an vote up if solution helped so others can find it easily.
With pleasure !
Stackblitz not found
Important to note: leave it as a variable (myFilter) instead of refactoring it to a method/function. Otherwise the this keyword goes out of scope.
2

You can use [min] and [max] for this.

Component.html

<mat-form-field class="example-full-width">
  <input matInput [min]="minDate" [max]="maxDate" [matDatepicker]="picker" placeholder="Choose a date">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

Component.ts

minDate = new Date(2000, 0, 1);
maxDate = new Date(2020, 0, 1);

1 Comment

Hello, I already use [min] and [max] for the duration of the contract of my employees, but I do not know how to add also the dates of unavailability (leave, illness)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.