From 78af7af8c6721fa66e5a51d8ea99cc62f9ebc710 Mon Sep 17 00:00:00 2001 From: Kristiyan Kostadinov Date: Sun, 29 Sep 2024 09:11:45 +0200 Subject: [PATCH] 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`. --- .../core/datetime/native-date-adapter.ts | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/src/material/core/datetime/native-date-adapter.ts b/src/material/core/datetime/native-date-adapter.ts index 73d953c7298c..9f951a92a08c 100644 --- a/src/material/core/datetime/native-date-adapter.ts +++ b/src/material/core/datetime/native-date-adapter.ts @@ -89,7 +89,24 @@ export class NativeDateAdapter extends DateAdapter { } getFirstDayOfWeek(): number { - // We can't tell using native JS Date what the first day of the week is, we default to Sunday. + if (typeof Intl !== 'undefined' && Intl.Locale) { + const locale = new Intl.Locale(this.locale) as Intl.Locale & { + getWeekInfo?: () => {firstDay: number}; + weekInfo?: {firstDay: number}; + }; + + // Some browsers implement a `getWeekInfo` method while others have a `weekInfo` getter. + // Note that this isn't supported in all browsers so we need to null check it. + const firstDay = (locale.getWeekInfo?.() || locale.weekInfo)?.firstDay; + + if (firstDay != null) { + // `weekInfo.firstDay` is a number between 1 and 7 where, starting from Monday, + // whereas our representation is 0 to 6 where 0 is Sunday so we need to normalize it. + return firstDay === 7 ? 0 : firstDay; + } + } + + // Default to Sunday if the browser doesn't provide the week information. return 0; }