isValid.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. "use strict";
  2. exports.isValid = isValid;
  3. var _index = require("./isDate.js");
  4. var _index2 = require("./toDate.js");
  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. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  18. *
  19. * @param date - The date to check
  20. *
  21. * @returns The date is valid
  22. *
  23. * @example
  24. * // For the valid date:
  25. * const result = isValid(new Date(2014, 1, 31))
  26. * //=> true
  27. *
  28. * @example
  29. * // For the value, convertable into a date:
  30. * const result = isValid(1393804800000)
  31. * //=> true
  32. *
  33. * @example
  34. * // For the invalid date:
  35. * const result = isValid(new Date(''))
  36. * //=> false
  37. */
  38. function isValid(date) {
  39. if (!(0, _index.isDate)(date) && typeof date !== "number") {
  40. return false;
  41. }
  42. const _date = (0, _index2.toDate)(date);
  43. return !isNaN(Number(_date));
  44. }