Skip to content

Commit

Permalink
fix(isDate): enhance Date declaration compatibility across multiple e…
Browse files Browse the repository at this point in the history
…nvironments (#2231)
  • Loading branch information
CiprianS authored Jun 12, 2023
1 parent 9ba1735 commit df1351a
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 1 deletion.
20 changes: 19 additions & 1 deletion src/lib/isDate.js
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,25 @@ export default function isDate(input, options) {
dateObj[formatWord.charAt(0)] = dateWord;
}

return new Date(`${dateObj.m}/${dateObj.d}/${dateObj.y}`).getDate() === +dateObj.d;
let fullYear = dateObj.y;

if (dateObj.y.length === 2) {
const parsedYear = parseInt(dateObj.y, 10);

if (isNaN(parsedYear)) {
return false;
}

const currentYearLastTwoDigits = new Date().getFullYear() % 100;

if (parsedYear < currentYearLastTwoDigits) {
fullYear = `20${dateObj.y}`;
} else {
fullYear = `19${dateObj.y}`;
}
}

return new Date(`${fullYear}-${dateObj.m}-${dateObj.d}`).getDate() === +dateObj.d;
}

if (!options.strictMode) {
Expand Down
2 changes: 2 additions & 0 deletions test/validators.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12803,6 +12803,7 @@ describe('Validators', () => {
'15/7/2002',
'15-7-2002',
'15/07-02',
'30/04/--',
],
});
test({
Expand All @@ -12817,6 +12818,7 @@ describe('Validators', () => {
'15/7/02',
'15-7-02',
'5/7-02',
'3/4/aa',
],
});
test({
Expand Down

0 comments on commit df1351a

Please sign in to comment.