Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix locale based weekday names
Browse files Browse the repository at this point in the history
mark-tate committed Nov 5, 2024

Verified

This commit was created on GitHub.com and signed with GitHub’s verified signature.
1 parent 8221fe4 commit 192cc69
Showing 2 changed files with 18 additions and 14 deletions.
19 changes: 7 additions & 12 deletions packages/lab/src/date-adapters/date-fns.ts
Original file line number Diff line number Diff line change
@@ -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<Date, Locale> {
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.
13 changes: 11 additions & 2 deletions packages/lab/src/date-adapters/dayjs.ts
Original file line number Diff line number Diff line change
@@ -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<Dayjs, string> {
* 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];
}

/**

0 comments on commit 192cc69

Please sign in to comment.