Skip to content

Commit 57a0b91

Browse files
committed
fix(material/core): infer first day of week in native date adapter
Some browsers provide information about the first day of the week so we can infer it in the `NativeDateAdapter`.
1 parent ff3d342 commit 57a0b91

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

src/material/core/datetime/native-date-adapter.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,24 @@ export class NativeDateAdapter extends DateAdapter<Date> {
8989
}
9090

9191
getFirstDayOfWeek(): number {
92-
// We can't tell using native JS Date what the first day of the week is, we default to Sunday.
92+
// At the time of writing `Intl.Locale` isn't available
93+
// in the internal types so we need to cast to `any`.
94+
if (typeof Intl !== 'undefined' && (Intl as any).Locale) {
95+
const locale = new (Intl as any).Locale(this.locale) as {
96+
getWeekInfo?: () => {firstDay: number};
97+
weekInfo?: {firstDay: number};
98+
};
99+
100+
// Some browsers implement a `getWeekInfo` method while others have a `weekInfo` getter.
101+
// Note that this isn't supported in all browsers so we need to null check it.
102+
const firstDay = (locale.getWeekInfo?.() || locale.weekInfo)?.firstDay ?? 0;
103+
104+
// `weekInfo.firstDay` is a number between 1 and 7 where, starting from Monday,
105+
// whereas our representation is 0 to 6 where 0 is Sunday so we need to normalize it.
106+
return firstDay === 7 ? 0 : firstDay;
107+
}
108+
109+
// Default to Sunday if the browser doesn't provide the week information.
93110
return 0;
94111
}
95112

0 commit comments

Comments
 (0)