Block weekends in the Angular Material datepicker

Asked

Viewed 379 times

0

I am using the Datepicker of Angular Material and need to block Saturdays and Sundays in order not to be selected. How can I do this ?

Code:

ts

import {Component} from '@angular/core';

/** @title Basic datepicker */
@Component({
  selector: 'datepicker-overview-example',
  templateUrl: 'datepicker-overview-example.html',
  styleUrls: ['datepicker-overview-example.css'],
})
export class DatepickerOverviewExample {}

html

<mat-form-field>
  <input matInput [matDatepicker]="picker" placeholder="Choose a date">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

Imagery:

inserir a descrição da imagem aqui

  • An option for handling this component is https://momentjs.com/ check the documentation, there are other options also that may be very useful in the future.

1 answer

4


<mat-form-field>
  <input matInput [matDatepickerFilter]="myFilter"  [matDatepicker]="picker" placeholder="Choose a date">
  <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle>
  <mat-datepicker #picker></mat-datepicker>
</mat-form-field>

TS

  myFilter = (d: Date): boolean => {
    const day = d.getDay();
    // Prevenir Sabado e Domingo
    return day !== 0 && day !== 6;
  }

Browser other questions tagged

You are not signed in. Login or sign up in order to post.