index.js 1.2 KB

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