@@ -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