index.js 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = isValid;
  6. var _index = _interopRequireDefault(require("../toDate/index.js"));
  7. var _index2 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * @name isValid
  11. * @category Common Helpers
  12. * @summary Is the given date valid?
  13. *
  14. * @description
  15. * Returns false if argument is Invalid Date and true otherwise.
  16. * Argument is converted to Date using `toDate`. See [toDate]{@link https://date-fns.org/docs/toDate}
  17. * Invalid Date is a Date, whose time value is NaN.
  18. *
  19. * Time value of Date: http://es5.github.io/#x15.9.1.1
  20. *
  21. * ### v2.0.0 breaking changes:
  22. *
  23. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  24. *
  25. * - Now `isValid` doesn't throw an exception
  26. * if the first argument is not an instance of Date.
  27. * Instead, argument is converted beforehand using `toDate`.
  28. *
  29. * Examples:
  30. *
  31. * | `isValid` argument | Before v2.0.0 | v2.0.0 onward |
  32. * |---------------------------|---------------|---------------|
  33. * | `new Date()` | `true` | `true` |
  34. * | `new Date('2016-01-01')` | `true` | `true` |
  35. * | `new Date('')` | `false` | `false` |
  36. * | `new Date(1488370835081)` | `true` | `true` |
  37. * | `new Date(NaN)` | `false` | `false` |
  38. * | `'2016-01-01'` | `TypeError` | `false` |
  39. * | `''` | `TypeError` | `false` |
  40. * | `1488370835081` | `TypeError` | `true` |
  41. * | `NaN` | `TypeError` | `false` |
  42. *
  43. * We introduce this change to make *date-fns* consistent with ECMAScript behavior
  44. * that try to coerce arguments to the expected type
  45. * (which is also the case with other *date-fns* functions).
  46. *
  47. * @param {*} date - the date to check
  48. * @returns {Boolean} the date is valid
  49. * @throws {TypeError} 1 argument required
  50. *
  51. * @example
  52. * // For the valid date:
  53. * var result = isValid(new Date(2014, 1, 31))
  54. * //=> true
  55. *
  56. * @example
  57. * // For the value, convertable into a date:
  58. * var result = isValid(1393804800000)
  59. * //=> true
  60. *
  61. * @example
  62. * // For the invalid date:
  63. * var result = isValid(new Date(''))
  64. * //=> false
  65. */
  66. function isValid(dirtyDate) {
  67. (0, _index2.default)(1, arguments);
  68. var date = (0, _index.default)(dirtyDate);
  69. return !isNaN(date);
  70. }
  71. module.exports = exports.default;