isValid.js 1020 B

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