%PDF- %PDF-
Direktori : /var/www/html/node_modules/date-fns/esm/isValid/ |
Current File : /var/www/html/node_modules/date-fns/esm/isValid/index.js |
import isDate from "../isDate/index.js"; import toDate from "../toDate/index.js"; import requiredArgs from "../_lib/requiredArgs/index.js"; /** * @name isValid * @category Common Helpers * @summary Is the given date valid? * * @description * Returns false if argument is Invalid Date and true otherwise. * Argument is converted to Date using `toDate`. See [toDate]{@link https://date-fns.org/docs/toDate} * Invalid Date is a Date, whose time value is NaN. * * Time value of Date: http://es5.github.io/#x15.9.1.1 * * ### v2.0.0 breaking changes: * * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes). * * - Now `isValid` doesn't throw an exception * if the first argument is not an instance of Date. * Instead, argument is converted beforehand using `toDate`. * * Examples: * * | `isValid` argument | Before v2.0.0 | v2.0.0 onward | * |---------------------------|---------------|---------------| * | `new Date()` | `true` | `true` | * | `new Date('2016-01-01')` | `true` | `true` | * | `new Date('')` | `false` | `false` | * | `new Date(1488370835081)` | `true` | `true` | * | `new Date(NaN)` | `false` | `false` | * | `'2016-01-01'` | `TypeError` | `false` | * | `''` | `TypeError` | `false` | * | `1488370835081` | `TypeError` | `true` | * | `NaN` | `TypeError` | `false` | * * We introduce this change to make *date-fns* consistent with ECMAScript behavior * that try to coerce arguments to the expected type * (which is also the case with other *date-fns* functions). * * @param {*} date - the date to check * @returns {Boolean} the date is valid * @throws {TypeError} 1 argument required * * @example * // For the valid date: * const result = isValid(new Date(2014, 1, 31)) * //=> true * * @example * // For the value, convertable into a date: * const result = isValid(1393804800000) * //=> true * * @example * // For the invalid date: * const result = isValid(new Date('')) * //=> false */ export default function isValid(dirtyDate) { requiredArgs(1, arguments); if (!isDate(dirtyDate) && typeof dirtyDate !== 'number') { return false; } var date = toDate(dirtyDate); return !isNaN(Number(date)); }