index.js 3.5 KB

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