@@ -7,17 +7,8 @@ function isLeapYear(year) {
77}
88
99function has53Week ( year ) {
10- const d = new Date ( Date . UTC ( year , 11 , 31 ) ) ; // Dec 31st
11- const UTCday = d . getUTCDay ( ) ;
12- // ISO weeks: week starts on Monday (1), ends Sunday (7)
13- // Dec 31 must be Thursday (4) or it's a leap year ending on Wednesday (3)
14- return UTCday === 4 || ( UTCday === 3 && isLeapYear ( year ) ) ;
15- }
16-
17- function lastDayofYear ( year ) {
18- const dec31 = new Date ( year , 11 , 31 ) ;
19- const lastDay = dec31 . getDay ( ) ;
20- return lastDay === 0 ? 7 : lastDay ;
10+ const jan1 = new Date ( Date . UTC ( year , 0 , 1 ) ) . getUTCDay ( ) ;
11+ return ( jan1 === 4 ) || ( isLeapYear ( year ) && jan1 === 3 ) ;
2112}
2213
2314const isValidIso8601 = ( str ) => {
@@ -30,17 +21,20 @@ const isValidIso8601 = (str) => {
3021 return false ;
3122 }
3223
24+ // Not checking time for ordinary dates and dates with weeks as its uncommon, but can be added in future as it is valid format.
3325 // need to check for ordinary dates
3426 const ordinalMatch = str . match ( / ^ ( \d { 4 } ) - ? ( \d { 3 } ) ( [ T ] { 1 } \. * | $ ) / ) ;
3527 if ( ordinalMatch ) {
3628 const oYear = Number ( ordinalMatch [ 1 ] ) ;
3729 const oDay = Number ( ordinalMatch [ 2 ] ) ;
30+ if ( oDay < 1 ) return false ;
3831 // if is leap year
3932 if ( isLeapYear ( oYear ) ) return oDay <= 366 ;
4033 return oDay <= 365 ;
4134 }
4235
4336 // need to check for dates with week, dates with week and day
37+ // only week match - issue if dates are with time weekmatch cannot reject it it will check only week part and return it, which i need to solve, same case for ordinal dates.
4438 const WeekMatch = str . match ( / ^ ( \d { 4 } ) - W ( \d { 2 } ) (?: - ( \d ) ) ? $ / ) ;
4539 if ( WeekMatch ) {
4640 const [ , yearStr , weekStr , dayStr ] = WeekMatch ;
@@ -52,8 +46,7 @@ const isValidIso8601 = (str) => {
5246 // check if week is last week of year it means 53 or 52, does it ends in between the last day
5347 // check if day exist if it does it is in correct range
5448 if ( day != null ) {
55- const maxValidDay = week === 53 || week === 52 ? lastDayofYear ( year ) : 7 ;
56- if ( day < 1 || day > maxValidDay ) return false ;
49+ if ( day < 1 || day > 7 ) return false ;
5750 }
5851 return true ;
5952 }
0 commit comments