feat(isDate): Enhance Date declaration compatibility across multiple environments by CiprianS · Pull Request #2231 · validatorjs/validator.js
feat(isDate): Enhance Date declaration compatibility across multiple environments
The issue occurred with the date declaration in the React Native - Android environment, specifically when using the following format:
new Date(${dateObj.m}/${dateObj.d}/${dateObj.y}).getDate() === +dateObj.d;
In JavaScript, the Date constructor parses a string argument in a platform-dependent way, and it may not parse the string identically across different environments, such as various browsers or operating systems. The date format ('mm/dd/yyyy') used in the current code is very U.S.-centric and isn't guaranteed to work consistently across all systems or locales.
According to the ECMAScript specification, the only string format required to be parsed accurately across all platforms is a variant of the ISO 8601 format: 'YYYY-MM-DDTHH:mm:ss.sssZ'. Any other format may produce inconsistent results, depending on the system's implementation.
In scenarios where the year is represented with two digits (e.g., 95, 00, 18), we have introduced a check to correctly prepend it with either '20' or '19', thus maintaining accuracy and compatibility.
This enhancement aims to increase the reliability of the isDate feature across multiple environments and bring the codebase closer to established standards.
Checklist
- PR contains only changes related; no stray files, etc.
- README updated (where applicable)
- Tests written (where applicable)
- References provided in PR (where applicable)