Actualmente tengo un rango de selector de fecha mate. La lógica es que la fecha mínima en el calendario que el usuario puede seleccionar es + 2 días, por lo que, por ejemplo, la fecha de hoy es 20 (20 de julio de 2022), luego la fecha mínima es 22 (22 de julio de 2022) porque es + 2.
Pero si los próximos 2 días son fines de semana, por ejemplo, nuestra fecha actual es, digamos que hoy es 22 (22 de julio de 2022) si observa el calendario 23 y 24 son fines de semana, por lo que debe excluirse, por lo que +2 comenzaría desde 22 ( 22 de julio de 2022) y 25 (25 de julio de 2022), por lo que la fecha mínima sería 26 (26 de julio de 2022).
¿Cómo manejaríamos esta lógica en un selector de fecha?
#html
<mat-form-field> <mat-label>Enter a date range</mat-label> <mat-date-range-input [rangePicker]="picker" [dateFilter]="weekendsDatesFilter"> <input matStartDate matInput placeholder="Start date" > <input matEndDate matInput placeholder="End date"> </mat-date-range-input> <mat-datepicker-toggle matSuffix [for]="picker"></mat-datepicker-toggle> <mat-date-range-picker #picker></mat-date-range-picker> </mat-form-field>#ts
export class DateRangePickerOverviewExample { currentDate = new Date(); ngOnInit(): void { this.currentDate = new Date(this.currentDate.setDate(this.currentDate.getDate() + 2)); } weekendsDatesFilter = (d: Date): boolean => { const day = d.getDay(); /* Prevent Saturday and Sunday for select. */ return day !== 0 && day !== 6; }; }¿Puedes comprobar esto haciendo clic aquí ? Espero que esto funcione para tí.
import { Component } from '@angular/core'; @Component({ selector: 'date-range-picker-overview-example', templateUrl: 'date-range-picker-overview-example.html', styleUrls: ['date-range-picker-overview-example.css'], }) export class DateRangePickerOverviewExample { currentDate = new Date(); ngOnInit(): void { this.currentDate = new Date( this.currentDate.setDate(this.currentDate.getDate() + 2) ); !this.weekendsDatesFilter(this.currentDate) && this.weekendsHandler(); } weekendsDatesFilter = (d: Date): boolean => { const day = d.getDay(); /* Prevent Saturday and Sunday for select. */ return day !== 0 && day !== 6; }; weekendsHandler() { this.currentDate = new Date( this.currentDate.setDate(this.currentDate.getDate() + 1) ); !this.weekendsDatesFilter(this.currentDate) && this.weekendsHandler(); } }