From df1351a2ac8059350d4e723afc0f859e22a3ab61 Mon Sep 17 00:00:00 2001 From: Ciprian Stoleru Date: Mon, 12 Jun 2023 06:59:50 +0300 Subject: [PATCH] fix(isDate): enhance Date declaration compatibility across multiple environments (#2231) --- src/lib/isDate.js | 20 +++++++++++++++++++- test/validators.test.js | 2 ++ 2 files changed, 21 insertions(+), 1 deletion(-) diff --git a/src/lib/isDate.js b/src/lib/isDate.js index 8b7862e8b..9f1c6926b 100644 --- a/src/lib/isDate.js +++ b/src/lib/isDate.js @@ -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) { diff --git a/test/validators.test.js b/test/validators.test.js index 937b52a26..655f370b2 100644 --- a/test/validators.test.js +++ b/test/validators.test.js @@ -12803,6 +12803,7 @@ describe('Validators', () => { '15/7/2002', '15-7-2002', '15/07-02', + '30/04/--', ], }); test({ @@ -12817,6 +12818,7 @@ describe('Validators', () => { '15/7/02', '15-7-02', '5/7-02', + '3/4/aa', ], }); test({