Created
February 25, 2026 19:09
-
-
Save petergi/5dbe33a421076c1650b693768ce62231 to your computer and use it in GitHub Desktop.
Checks if a valid date object can be created from the given values. - Use the spread operator (`...`) to pass the array of arguments to the `Date` constructor.
- Use `Date.prototype.valueOf()` and `Number.isNaN()` to check if a valid `Date` object can be created from the given values.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Checks if a valid date object can be created from the given values. | |
| - Use the spread operator (`...`) to pass the array of arguments to the `Date` constructor. | |
| - Use `Date.prototype.valueOf()` and `Number.isNaN()` to check if a valid `Date` object can be created from the given values. | |
| const isDateValid = (...val) => !Number.isNaN(new Date(...val).valueOf()); | |
| isDateValid('December 17, 1995 03:24:00'); // true | |
| isDateValid('1995-12-17T03:24:00'); // true | |
| isDateValid('1995-12-17 T03:24:00'); // false | |
| isDateValid('Duck'); // false | |
| isDateValid(1995, 11, 17); // true | |
| isDateValid(1995, 11, 17, 'Duck'); // false | |
| isDateValid({}); // false | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment