UNPKG

1.02 kBJavaScriptView Raw
1import { isDate } from "./isDate.js";
2import { toDate } from "./toDate.js";
3
4/**
5 * @name isValid
6 * @category Common Helpers
7 * @summary Is the given date valid?
8 *
9 * @description
10 * Returns false if argument is Invalid Date and true otherwise.
11 * Argument is converted to Date using `toDate`. See [toDate](https://date-fns.org/docs/toDate)
12 * Invalid Date is a Date, whose time value is NaN.
13 *
14 * Time value of Date: http://es5.github.io/#x15.9.1.1
15 *
16 * @param date - The date to check
17 *
18 * @returns The date is valid
19 *
20 * @example
21 * // For the valid date:
22 * const result = isValid(new Date(2014, 1, 31))
23 * //=> true
24 *
25 * @example
26 * // For the value, convertible into a date:
27 * const result = isValid(1393804800000)
28 * //=> true
29 *
30 * @example
31 * // For the invalid date:
32 * const result = isValid(new Date(''))
33 * //=> false
34 */
35export function isValid(date) {
36 return !((!isDate(date) && typeof date !== "number") || isNaN(+toDate(date)));
37}
38
39// Fallback for modularized imports:
40export default isValid;