index.js 865 B

1234567891011121314151617181920212223242526272829303132333435
  1. var isDate = require('../is_date/index.js')
  2. /**
  3. * @category Common Helpers
  4. * @summary Is the given date valid?
  5. *
  6. * @description
  7. * Returns false if argument is Invalid Date and true otherwise.
  8. * Invalid Date is a Date, whose time value is NaN.
  9. *
  10. * Time value of Date: http://es5.github.io/#x15.9.1.1
  11. *
  12. * @param {Date} date - the date to check
  13. * @returns {Boolean} the date is valid
  14. * @throws {TypeError} argument must be an instance of Date
  15. *
  16. * @example
  17. * // For the valid date:
  18. * var result = isValid(new Date(2014, 1, 31))
  19. * //=> true
  20. *
  21. * @example
  22. * // For the invalid date:
  23. * var result = isValid(new Date(''))
  24. * //=> false
  25. */
  26. function isValid (dirtyDate) {
  27. if (isDate(dirtyDate)) {
  28. return !isNaN(dirtyDate)
  29. } else {
  30. throw new TypeError(toString.call(dirtyDate) + ' is not an instance of Date')
  31. }
  32. }
  33. module.exports = isValid