index.js 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. var parse = require('../parse/index.js')
  2. /**
  3. * @category Range Helpers
  4. * @summary Is the given date within the range?
  5. *
  6. * @description
  7. * Is the given date within the range?
  8. *
  9. * @param {Date|String|Number} date - the date to check
  10. * @param {Date|String|Number} startDate - the start of range
  11. * @param {Date|String|Number} endDate - the end of range
  12. * @returns {Boolean} the date is within the range
  13. * @throws {Error} startDate cannot be after endDate
  14. *
  15. * @example
  16. * // For the date within the range:
  17. * isWithinRange(
  18. * new Date(2014, 0, 3), new Date(2014, 0, 1), new Date(2014, 0, 7)
  19. * )
  20. * //=> true
  21. *
  22. * @example
  23. * // For the date outside of the range:
  24. * isWithinRange(
  25. * new Date(2014, 0, 10), new Date(2014, 0, 1), new Date(2014, 0, 7)
  26. * )
  27. * //=> false
  28. */
  29. function isWithinRange (dirtyDate, dirtyStartDate, dirtyEndDate) {
  30. var time = parse(dirtyDate).getTime()
  31. var startTime = parse(dirtyStartDate).getTime()
  32. var endTime = parse(dirtyEndDate).getTime()
  33. if (startTime > endTime) {
  34. throw new Error('The start of the range cannot be after the end of the range')
  35. }
  36. return time >= startTime && time <= endTime
  37. }
  38. module.exports = isWithinRange