isValid.cjs 1.0 KB

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