index.js 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import toDate from "../toDate/index.js";
  2. import requiredArgs from "../_lib/requiredArgs/index.js";
  3. /**
  4. * @name isWithinInterval
  5. * @category Interval Helpers
  6. * @summary Is the given date within the interval?
  7. *
  8. * @description
  9. * Is the given date within the interval? (Including start and end.)
  10. *
  11. * ### v2.0.0 breaking changes:
  12. *
  13. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  14. *
  15. * - The function was renamed from `isWithinRange` to `isWithinInterval`.
  16. * This change was made to mirror the use of the word "interval" in standard ISO 8601:2004 terminology:
  17. *
  18. * ```
  19. * 2.1.3
  20. * time interval
  21. * part of the time axis limited by two instants
  22. * ```
  23. *
  24. * Also, this function now accepts an object with `start` and `end` properties
  25. * instead of two arguments as an interval.
  26. * This function now throws `RangeError` if the start of the interval is after its end
  27. * or if any date in the interval is `Invalid Date`.
  28. *
  29. * ```javascript
  30. * // Before v2.0.0
  31. *
  32. * isWithinRange(
  33. * new Date(2014, 0, 3),
  34. * new Date(2014, 0, 1), new Date(2014, 0, 7)
  35. * )
  36. *
  37. * // v2.0.0 onward
  38. *
  39. * isWithinInterval(
  40. * new Date(2014, 0, 3),
  41. * { start: new Date(2014, 0, 1), end: new Date(2014, 0, 7) }
  42. * )
  43. * ```
  44. *
  45. * @param {Date|Number} date - the date to check
  46. * @param {Interval} interval - the interval to check
  47. * @returns {Boolean} the date is within the interval
  48. * @throws {TypeError} 2 arguments required
  49. * @throws {RangeError} The start of an interval cannot be after its end
  50. * @throws {RangeError} Date in interval cannot be `Invalid Date`
  51. *
  52. * @example
  53. * // For the date within the interval:
  54. * isWithinInterval(new Date(2014, 0, 3), {
  55. * start: new Date(2014, 0, 1),
  56. * end: new Date(2014, 0, 7)
  57. * })
  58. * //=> true
  59. *
  60. * @example
  61. * // For the date outside of the interval:
  62. * isWithinInterval(new Date(2014, 0, 10), {
  63. * start: new Date(2014, 0, 1),
  64. * end: new Date(2014, 0, 7)
  65. * })
  66. * //=> false
  67. *
  68. * @example
  69. * // For date equal to interval start:
  70. * isWithinInterval(date, { start, end: date }) // => true
  71. *
  72. * @example
  73. * // For date equal to interval end:
  74. * isWithinInterval(date, { start: date, end }) // => true
  75. */
  76. export default function isWithinInterval(dirtyDate, interval) {
  77. requiredArgs(2, arguments);
  78. var time = toDate(dirtyDate).getTime();
  79. var startTime = toDate(interval.start).getTime();
  80. var endTime = toDate(interval.end).getTime(); // Throw an exception if start date is after end date or if any date is `Invalid Date`
  81. if (!(startTime <= endTime)) {
  82. throw new RangeError('Invalid interval');
  83. }
  84. return time >= startTime && time <= endTime;
  85. }