Skip to content

fix: correct getDayOfWeekCode to return day of week instead of day of month#6254

Open
coding-shalabh wants to merge 1 commit intoHacker0x01:mainfrom
coding-shalabh:fix/get-day-of-week-code
Open

fix: correct getDayOfWeekCode to return day of week instead of day of month#6254
coding-shalabh wants to merge 1 commit intoHacker0x01:mainfrom
coding-shalabh:fix/get-day-of-week-code

Conversation

@coding-shalabh
Copy link
Copy Markdown

Summary

Fixes #6217 - getDayOfWeekCode in date_utils has incorrect behavior

Problem

The current implementation of getDayOfWeekCode() returns the day of the month instead of the day of week code. This happens because the format string ddd is used with date-fns, which represents the day of month with ordinal (1st, 2nd, 3rd, etc.) rather than the day of week abbreviation.

Current code:

export function getDayOfWeekCode(day: Date, locale?: Locale): string {
  return formatDate(day, "ddd", locale);
}

For example, calling this on the 15th of any month would return "15" instead of the expected day code like "mon" or "tue".

Solution

Changed the format string to eee which correctly returns the abbreviated day of week name, using the enUS locale to ensure consistency, and converting to lowercase to match the expected output format.

Fixed code:

export function getDayOfWeekCode(day: Date, locale?: Locale): string {
  return formatDate(day, "eee", enUS).toLowerCase();
}

This now correctly returns lowercase day-of-week codes: sun, mon, tue, wed, thu, fri, sat

Reference

  • date-fns format documentation: https://date-fns.org/docs/format
    • ddd = ordinal day of month (1st, 2nd, 3rd, etc.)
    • eee = abbreviated day of week (Sun, Mon, Tue, etc.)

Testing

  • The fix ensures getDayOfWeekCode returns the correct day-of-week codes
  • Uses enUS locale for consistent output across different locales
  • Converts to lowercase to match expected format

🤖 Generated with Claude Code

Co-Authored-By: Claude noreply@anthropic.com

… month

Fixes Hacker0x01#6217

Changed getDayOfWeekCode() to use format 'eee' instead of 'ddd' with enUS locale.

The previous implementation used format 'ddd' which returns the day of the month (1-31)
instead of the day of week code (sun, mon, tue, etc.). This was causing incorrect behavior
when the function was used to determine day-of-week codes for calendar operations.

As per date-fns documentation:
- 'ddd' = day of month with ordinal (1st, 2nd, etc.)
- 'eee' = day of week abbreviated (Sun, Mon, Tue, etc.)

The fix uses 'eee' with enUS locale and converts to lowercase to return the expected
day-of-week codes: sun, mon, tue, wed, thu, fri, sat.

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

getDayOfWeekCode in date_utils has incorrect behavior.

1 participant