Skip to content

Commit 8d61a59

Browse files
committed
fix(Calendar): use correct year for week selection
- Use Thursday of the week to determine the correct ISO week year - Fixes weeks showing the wrong year in January transitions - Resolves 2024W1 appearing instead of 2025W1 for first week
1 parent 89fcd56 commit 8d61a59

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed

js/src/calendar.js

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -620,13 +620,8 @@ class Calendar extends BaseComponent {
620620
</tr>
621621
</thead>` : ''}
622622
<tbody>
623-
${this._view === 'days' ? monthDetails.map(week => {
624-
const date = convertToDateObject(
625-
week.weekNumber === 0 ?
626-
`${calendarDate.getFullYear()}W53` :
627-
`${calendarDate.getFullYear()}W${week.weekNumber}`,
628-
this._config.selectionType
629-
)
623+
${this._view === 'days' ? monthDetails.map(({ week, days }) => {
624+
const { date } = days[0]
630625
const rowAttributes = this._rowWeekAttributes(date)
631626
return (
632627
`<tr
@@ -635,9 +630,9 @@ class Calendar extends BaseComponent {
635630
${rowAttributes.ariaSelected ? 'aria-selected="true"' : ''}
636631
>
637632
${this._config.showWeekNumber ?
638-
`<th class="calendar-cell-week-number">${week.weekNumber === 0 ? 53 : week.weekNumber}</td>` : ''
633+
`<th class="calendar-cell-week-number">${week.number}</td>` : ''
639634
}
640-
${week.days.map(({ date, month }) => {
635+
${days.map(({ date, month }) => {
641636
const cellAttributes = this._cellDayAttributes(date, month)
642637
return month === 'current' || this._config.showAdjacementDays ?
643638
`<td

js/src/util/calendar.js

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,8 @@ export const getDateBySelectionType = (date, selectionType) => {
100100
}
101101

102102
if (selectionType === 'week') {
103-
return `${date.getFullYear()}W${getWeekNumber(date)}`
103+
const { year, weekNumber } = getISOWeekNumberAndYear(date)
104+
return `${year}W${weekNumber.toString().padStart(2, '0')}`
104105
}
105106

106107
if (selectionType === 'month') {
@@ -263,11 +264,19 @@ const getTrailingDays = (year, month, leadingDays, monthDays) => {
263264
}
264265

265266
/**
266-
* Calculates the ISO week number for a given date.
267-
* @param date - The date to calculate the week number for.
268-
* @returns The ISO week number.
267+
* Calculates the ISO 8601 week number and year for a given date.
268+
*
269+
* In the ISO 8601 standard:
270+
* - Weeks start on Monday.
271+
* - The first week of the year is the one that contains January 4th.
272+
* - The year of the week may differ from the calendar year (e.g., Dec 29, 2025 is in ISO year 2026).
273+
*
274+
* @param {Date} date - The date for which to calculate the ISO week number and year.
275+
* @returns {{ weekNumber: number, year: number }} An object containing:
276+
* - `weekNumber`: the ISO week number (1–53),
277+
* - `year`: the ISO year (may differ from the calendar year of the date).
269278
*/
270-
export const getWeekNumber = date => {
279+
export const getISOWeekNumberAndYear = date => {
271280
const tempDate = new Date(date)
272281
tempDate.setHours(0, 0, 0, 0)
273282

@@ -277,10 +286,9 @@ export const getWeekNumber = date => {
277286
const week1 = new Date(tempDate.getFullYear(), 0, 4)
278287

279288
// Calculate full weeks to the date
280-
const weekNumber =
281-
1 + Math.round((tempDate.getTime() - week1.getTime()) / 86_400_000 / 7)
289+
const weekNumber = 1 + Math.round((tempDate.getTime() - week1.getTime()) / 86_400_000 / 7)
282290

283-
return weekNumber
291+
return { weekNumber, year: tempDate.getFullYear() }
284292
}
285293

286294
/**
@@ -305,12 +313,14 @@ export const getMonthDetails = (year, month, firstDayOfWeek) => {
305313
for (const [index, day] of days.entries()) {
306314
if (index % 7 === 0 || weeks.length === 0) {
307315
weeks.push({
316+
week: { number: 0, year: 0 },
308317
days: []
309318
})
310319
}
311320

312321
if ((index + 1) % 7 === 0) {
313-
weeks[weeks.length - 1].weekNumber = getWeekNumber(day.date)
322+
const { weekNumber, year } = getISOWeekNumberAndYear(day.date)
323+
weeks[weeks.length - 1].week = { number: weekNumber, year }
314324
}
315325

316326
weeks[weeks.length - 1].days.push(day)

0 commit comments

Comments
 (0)