From 192cc693147740e6b380b130d47a01fcc1e5acb8 Mon Sep 17 00:00:00 2001 From: mark-tate <143323+mark-tate@users.noreply.github.com> Date: Tue, 5 Nov 2024 21:39:39 +0000 Subject: [PATCH] fix locale based weekday names --- packages/lab/src/date-adapters/date-fns.ts | 19 +++++++------------ packages/lab/src/date-adapters/dayjs.ts | 13 +++++++++++-- 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/packages/lab/src/date-adapters/date-fns.ts b/packages/lab/src/date-adapters/date-fns.ts index 795d370b7cd..aff3160cdbe 100644 --- a/packages/lab/src/date-adapters/date-fns.ts +++ b/packages/lab/src/date-adapters/date-fns.ts @@ -1,6 +1,7 @@ import { type Locale, add as addDateFns, + addDays as addDaysFns, addMilliseconds as addMillisecondsDateFns, differenceInMilliseconds, endOfDay, @@ -462,22 +463,16 @@ export class AdapterDateFns implements SaltDateAdapter { public getDayOfWeekName( dow: number, format: "long" | "short" | "narrow", - locale: Locale = enUS, + locale: Locale, ): string { - // Create a date representing the first day of the current week - const today = new Date(); - const currentDay = today.getDay(); - const diff = dow - currentDay; - const targetDate = new Date(today); - targetDate.setDate(today.getDate() + diff); - - // Use Intl.DateTimeFormat to get the day name - const options: Intl.DateTimeFormatOptions = { + const startOfWeekDate = startOfWeek(new Date(), { locale: locale ?? this.locale }); + const targetDate = addDaysFns(startOfWeekDate, dow); + return new Intl.DateTimeFormat( locale?.code ?? this.locale?.code, { weekday: format, - }; - return targetDate.toLocaleDateString(locale.code, options); + }).format(targetDate); } + /** * Gets the day of the month for a Date object. * @param date - The Date object. diff --git a/packages/lab/src/date-adapters/dayjs.ts b/packages/lab/src/date-adapters/dayjs.ts index 1833fdffeae..c204cfbfa8c 100644 --- a/packages/lab/src/date-adapters/dayjs.ts +++ b/packages/lab/src/date-adapters/dayjs.ts @@ -3,6 +3,7 @@ import customParseFormat from "dayjs/plugin/customParseFormat"; import timezone from "dayjs/plugin/timezone"; import utc from "dayjs/plugin/utc"; import weekday from "dayjs/plugin/weekday"; +import localeData from 'dayjs/plugin/localeData'; import type { AdapterOptions, RecommendedFormats, @@ -31,6 +32,7 @@ declare module "./types" { defaultDayjs.extend(utc); defaultDayjs.extend(timezone); defaultDayjs.extend(weekday); +defaultDayjs.extend(localeData); // Dayjs expects Title-case months, so treats "jun" as invalid function capitalizeMonthInDate(value: string, format: string) { @@ -500,14 +502,21 @@ export class AdapterDayjs implements SaltDateAdapter { * Gets the name of the day of the week. * @param dow - The day of the week as a number (0-6). * @param format - The format for the day name ("long", "short", "narrow"). + * @param locale - The locale to use * @returns The name of the day of the week. */ public getDayOfWeekName( dow: number, format: "long" | "short" | "narrow", + locale?: string ): string { - const day = this.dayjs().weekday(dow); - return format === "narrow" ? day.format("dd")[0] : day.format("dddd"); + const dayjsInstance = this.dayjs().locale(locale || this.locale); + if (format === "narrow") { + return dayjsInstance.localeData().weekdaysMin()[dow]; + } else if (format === "short") { + return dayjsInstance.localeData().weekdaysShort()[dow]; + } + return dayjsInstance.localeData().weekdays()[dow]; } /**