index.js 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. import requiredArgs from "../_lib/requiredArgs/index.js";
  2. /**
  3. * @name isDate
  4. * @category Common Helpers
  5. * @summary Is the given value a date?
  6. *
  7. * @description
  8. * Returns true if the given value is an instance of Date. The function works for dates transferred across iframes.
  9. *
  10. * ### v2.0.0 breaking changes:
  11. *
  12. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  13. *
  14. * @param {*} value - the value to check
  15. * @returns {boolean} true if the given value is a date
  16. * @throws {TypeError} 1 arguments required
  17. *
  18. * @example
  19. * // For a valid date:
  20. * const result = isDate(new Date())
  21. * //=> true
  22. *
  23. * @example
  24. * // For an invalid date:
  25. * const result = isDate(new Date(NaN))
  26. * //=> true
  27. *
  28. * @example
  29. * // For some value:
  30. * const result = isDate('2014-02-31')
  31. * //=> false
  32. *
  33. * @example
  34. * // For an object:
  35. * const result = isDate({})
  36. * //=> false
  37. */
  38. export default function isDate(value) {
  39. requiredArgs(1, arguments);
  40. return value instanceof Date || typeof value === 'object' && Object.prototype.toString.call(value) === '[object Date]';
  41. }