Skip to content

Commit 7b1d476

Browse files
authored
Merge pull request #1 from jesroffrouk/bugfixTesting
changed logic for has53week
2 parents 8bb4e28 + 98fbe31 commit 7b1d476

2 files changed

Lines changed: 7 additions & 13 deletions

File tree

src/lib/isISO8601.js

Lines changed: 6 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,8 @@ function isLeapYear(year) {
77
}
88

99
function 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

2314
const 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
}

test/validators.test.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11917,6 +11917,7 @@ describe('Validators', () => {
1191711917
'2023-W1',
1191811918
'2023-W01-',
1191911919
'2023-03-15T12:30:45.123+01',
11920+
'2000-000',
1192011921
];
1192111922

1192211923
it('should validate ISO 8601 dates', () => {

0 commit comments

Comments
 (0)