Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(material/core): infer first day of week in native date adapter #29802

Merged
merged 1 commit into from
Sep 30, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 18 additions & 1 deletion src/material/core/datetime/native-date-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,24 @@ export class NativeDateAdapter extends DateAdapter<Date> {
}

getFirstDayOfWeek(): number {
// We can't tell using native JS Date what the first day of the week is, we default to Sunday.
// At the time of writing `Intl.Locale` isn't available
// in the internal types so we need to cast to `any`.
if (typeof Intl !== 'undefined' && (Intl as any).Locale) {
const locale = new (Intl as any).Locale(this.locale) as {
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 ?? 0;

// `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;
}

Expand Down
Loading