Added a snippet isDateValid (#1415)

Co-authored-by: Isabelle Viktoria Maciohsek <maciv@hotmail.gr>
This commit is contained in:
Faruk Mollaoğlu
2020-10-08 16:39:23 +03:00
committed by GitHub
parent bb296e1d70
commit cd93e7edd2

23
snippets/isDateValid.md Normal file
View File

@ -0,0 +1,23 @@
---
title: isDateValid
tags: date,intermediate
---
Returns `true` if a valid date object can be created from the given values, `false` otherwise.
- 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.
```js
const isDateValid = (...val) => !isNaN(new Date(...val).valueOf());
```
```js
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
```