is it possible to hide the dates saturday/sunday in mat-datepicker. Or an alternative is rearrange them so it starts with monday
any help/suggestions are appreciated.
is it possible to hide the dates saturday/sunday in mat-datepicker. Or an alternative is rearrange them so it starts with monday
any help/suggestions are appreciated.
Well, You can't hide them. However you can make them disabled so that the user can be prevented from selecting any of the weekend dates.
You need to use the matDatepickerFilter
option to get this done.
In your datepicker-filter.html
<mat-form-field class="example-full-width">
<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>
In your datepicker-filter.ts have your myFilter
defined as,
@Component({
selector: 'datepicker-filter',
templateUrl: 'datepicker-filter.html',
styleUrls: ['datepicker-filter.css'],
})
export class DatepickerFilter {
myFilter = (d: Date): boolean => {
const day = d.getDay();
// Prevent Saturday and Sunday from being selected.
return day !== 0 && day !== 6;
}
}
Hope this helps!
Yes, yes it is!
To let it start on Monday (or any other day) I'll refer you to this: MatDatePicker start week on monday
But the real question is: how to hide them?
Simply put: add a custom class to specific dates.
In your component file:
@ViewChild("calendar", { static: false }) calendar: MatCalendar<Date>;
hideWeekends() {
return (date: Date): MatCalendarCellCssClasses => {
return this.calendar?.currentView === "month" && (date.getDay() === 0 || date.getDay() === 6) ? 'weekend' : '';
};
}
This function will be called on every date in the current view. In previous versions it appears this didn't run for the multi-year and year view, but in the current version it does, so you need to put a check to only do it for the month view. Otherwise years and months starting on Saturday/Sunday will be hidden as well in those other views. Other than that: it's just "apply the class to the 0th (Sunday) and 6th (Saturday) day of the week".
In your html:
<mat-calendar #calendar [dateClass]="hideWeekends()"></mat-calendar>
In your css:
.mat-calendar-table-header th:not(.mat-calendar-table-header-divider):first-child,
.mat-calendar-table-header th:not(.mat-calendar-table-header-divider):last-child,
.mat-calendar-body-cell.weekend {
display: none;
}
Hide the first and last column of the header (but not if it has the divider class), and all the cells that have the custom class.
If you did make a custom DateAdapter and the weekend is the last two columns you could alternatively do this for the header:
.mat-calendar-table-header th:not(.mat-calendar-table-header-divider):nth-last-child(-n+2)
Here's a full Stackblitz (the css doesn't get applied, but you can inspect the cells and see that the class is indeed applied, so it will work in your project): https://stackblitz.com/edit/angular-material-v12-rc-view-engine-14dwqp?file=src%2Fapp%2Fmatcalendar.ts
<input matInput [matDatepicker]="lastdatepicker"
[matDatepickerFilter]="weekendsDatesFilter" [min]="minlastDateOfProposedTrading"
[max]="lastDateOfProposedTradingDate" placeholder="DD/MM/YYYY"
formControlName="lastDateOfProposedTrading" required/>
weekendsDatesFilter = (d: any):boolean => {
const day = d.getDay();
return day !== 0 && day !== 6 ;
}