To get a Locale aware (in regards of start of the week) Material DatePicker i created this extension:
import { Injectable } from "@angular/core";
import { LOCALE_ID, Inject } from "@angular/core";
import { Platform } from "@angular/cdk/platform";
import { getLocaleFirstDayOfWeek } from "@angular/common";
import { NativeDateAdapter } from '@angular/material/core';
@Injectable({providedIn: 'root'})
export class LocaleDateAdapter extends NativeDateAdapter {
constructor(@Inject(LOCALE_ID) public locale: string) {
super(locale, new Platform());
}
getFirstDayOfWeek() {
return getLocaleFirstDayOfWeek(this.locale);
}
}
I also tried with 0 args constructor and constantly retuning 1 as first day of week. Some colleague told that {providedIn: 'root'} might help - it did not.
I hooked it in app.module.ts as provider:
{
provide: DateAdapter,
useClass: LocaleDateAdapter
}
my DatePicker is set up like this:
<mat-form-field appearance="fill">
<mat-label>set date</mat-label>
<input matInput [matDatepicker]="picker1" (dateChange)="onDateChange($event)" [formControl]=formDate>
<mat-datepicker-toggle matSuffix [for]="picker1"></mat-datepicker-toggle>
<mat-datepicker [startAt]="filter.date" #picker1></mat-datepicker>
</mat-form-field>
The problem is that my LocaleDateAdapter is not instantiated (Breakpoint not hit) and the start of the week isn't changing - should change to Monday.
How to make it work?