Skip to content

Commit 6a8c73d

Browse files
authored
fix(material/core): infer first day of week in native date adapter (#29802)
Some browsers provide information about the first day of the week so we can infer it in the `NativeDateAdapter`.
1 parent 86ebb9b commit 6a8c73d

File tree

1 file changed

+18
-1
lines changed

1 file changed

+18
-1
lines changed

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

+18-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,24 @@ export class NativeDateAdapter extends DateAdapter<Date> {
102102
}
103103

104104
getFirstDayOfWeek(): number {
105-
// We can't tell using native JS Date what the first day of the week is, we default to Sunday.
105+
// At the time of writing `Intl.Locale` isn't available
106+
// in the internal types so we need to cast to `any`.
107+
if (typeof Intl !== 'undefined' && (Intl as any).Locale) {
108+
const locale = new (Intl as any).Locale(this.locale) as {
109+
getWeekInfo?: () => {firstDay: number};
110+
weekInfo?: {firstDay: number};
111+
};
112+
113+
// Some browsers implement a `getWeekInfo` method while others have a `weekInfo` getter.
114+
// Note that this isn't supported in all browsers so we need to null check it.
115+
const firstDay = (locale.getWeekInfo?.() || locale.weekInfo)?.firstDay ?? 0;
116+
117+
// `weekInfo.firstDay` is a number between 1 and 7 where, starting from Monday,
118+
// whereas our representation is 0 to 6 where 0 is Sunday so we need to normalize it.
119+
return firstDay === 7 ? 0 : firstDay;
120+
}
121+
122+
// Default to Sunday if the browser doesn't provide the week information.
106123
return 0;
107124
}
108125

0 commit comments

Comments
 (0)