@@ -100,7 +100,8 @@ export const getDateBySelectionType = (date, selectionType) => {
100
100
}
101
101
102
102
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' ) } `
104
105
}
105
106
106
107
if ( selectionType === 'month' ) {
@@ -263,11 +264,19 @@ const getTrailingDays = (year, month, leadingDays, monthDays) => {
263
264
}
264
265
265
266
/**
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).
269
278
*/
270
- export const getWeekNumber = date => {
279
+ export const getISOWeekNumberAndYear = date => {
271
280
const tempDate = new Date ( date )
272
281
tempDate . setHours ( 0 , 0 , 0 , 0 )
273
282
@@ -277,10 +286,9 @@ export const getWeekNumber = date => {
277
286
const week1 = new Date ( tempDate . getFullYear ( ) , 0 , 4 )
278
287
279
288
// 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 )
282
290
283
- return weekNumber
291
+ return { weekNumber, year : tempDate . getFullYear ( ) }
284
292
}
285
293
286
294
/**
@@ -305,12 +313,14 @@ export const getMonthDetails = (year, month, firstDayOfWeek) => {
305
313
for ( const [ index , day ] of days . entries ( ) ) {
306
314
if ( index % 7 === 0 || weeks . length === 0 ) {
307
315
weeks . push ( {
316
+ week : { number : 0 , year : 0 } ,
308
317
days : [ ]
309
318
} )
310
319
}
311
320
312
321
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 }
314
324
}
315
325
316
326
weeks [ weeks . length - 1 ] . days . push ( day )
0 commit comments