summaryrefslogtreecommitdiff
path: root/date-fns/src/isValid/index.js
diff options
context:
space:
mode:
Diffstat (limited to 'date-fns/src/isValid/index.js')
-rw-r--r--date-fns/src/isValid/index.js66
1 files changed, 66 insertions, 0 deletions
diff --git a/date-fns/src/isValid/index.js b/date-fns/src/isValid/index.js
new file mode 100644
index 0000000..854c97e
--- /dev/null
+++ b/date-fns/src/isValid/index.js
@@ -0,0 +1,66 @@
+import toDate from '../toDate/index'
+import requiredArgs from '../_lib/requiredArgs/index'
+
+/**
+ * @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:
+ * var result = isValid(new Date(2014, 1, 31))
+ * //=> true
+ *
+ * @example
+ * // For the value, convertable into a date:
+ * var result = isValid(1393804800000)
+ * //=> true
+ *
+ * @example
+ * // For the invalid date:
+ * var result = isValid(new Date(''))
+ * //=> false
+ */
+export default function isValid(dirtyDate) {
+ requiredArgs(1, arguments)
+
+ var date = toDate(dirtyDate)
+ return !isNaN(date)
+}