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

Add isoWeekYear setter for Day.js ISO week handling #2800

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from

Conversation

FAL-coffee
Copy link

@FAL-coffee FAL-coffee commented Jan 3, 2025

This pull request introduces an isoWeekYear setter, allowing the ISO week year to be adjusted while preserving the current isoWeek and isoWeekday values. This change ensures consistent ISO-based date calculations and avoids unexpected behavior when using native .year() in combination with ISO week methods.

Background

In the current implementation, using .year(...) together with .isoWeek(...) can lead to unexpected results. For example:

import dayjs from 'dayjs'
import isoWeek from 'dayjs/plugin/isoWeek'
dayjs.extend(isoWeek)

// This snippet may produce an unexpected date (e.g., "2026-01-04")
// instead of the expected date in 2025.
const result = dayjs('2024-12-29')
  .year(2025)      // sets the local year, not the ISO year
  .isoWeek(1)
  .add(1, 'week')  // adds one week based on local calculations

console.log(result.format()) 
// Unintentionally outputs something like "2026-01-04"

This type of issue can recur in future years as well—for example, it is likely to occur again in years when December 31 belongs to the next year’s ISO week or January 1 still belongs to the previous year’s ISO week. A similar situation may arise in 2030 (or any year close to an ISO week boundary transition), leading to the same unexpected shift into the following calendar year.

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.

1 participant