index.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. import toDate from "../toDate/index.js";
  2. import requiredArgs from "../_lib/requiredArgs/index.js";
  3. /**
  4. * @name eachDayOfInterval
  5. * @category Interval Helpers
  6. * @summary Return the array of dates within the specified time interval.
  7. *
  8. * @description
  9. * Return the array of dates within the specified time interval.
  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 `eachDay` to `eachDayOfInterval`.
  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. * eachDay(new Date(2014, 0, 10), new Date(2014, 0, 20))
  33. *
  34. * // v2.0.0 onward
  35. *
  36. * eachDayOfInterval(
  37. * { start: new Date(2014, 0, 10), end: new Date(2014, 0, 20) }
  38. * )
  39. * ```
  40. *
  41. * @param {Interval} interval - the interval. See [Interval]{@link https://date-fns.org/docs/Interval}
  42. * @param {Object} [options] - an object with options.
  43. * @param {Number} [options.step=1] - the step to increment by. The value should be more than 1.
  44. * @returns {Date[]} the array with starts of days from the day of the interval start to the day of the interval end
  45. * @throws {TypeError} 1 argument required
  46. * @throws {RangeError} `options.step` must be a number greater than 1
  47. * @throws {RangeError} The start of an interval cannot be after its end
  48. * @throws {RangeError} Date in interval cannot be `Invalid Date`
  49. *
  50. * @example
  51. * // Each day between 6 October 2014 and 10 October 2014:
  52. * const result = eachDayOfInterval({
  53. * start: new Date(2014, 9, 6),
  54. * end: new Date(2014, 9, 10)
  55. * })
  56. * //=> [
  57. * // Mon Oct 06 2014 00:00:00,
  58. * // Tue Oct 07 2014 00:00:00,
  59. * // Wed Oct 08 2014 00:00:00,
  60. * // Thu Oct 09 2014 00:00:00,
  61. * // Fri Oct 10 2014 00:00:00
  62. * // ]
  63. */
  64. export default function eachDayOfInterval(dirtyInterval, options) {
  65. requiredArgs(1, arguments);
  66. var interval = dirtyInterval || {};
  67. var startDate = toDate(interval.start);
  68. var endDate = toDate(interval.end);
  69. var endTime = endDate.getTime(); // Throw an exception if start date is after end date or if any date is `Invalid Date`
  70. if (!(startDate.getTime() <= endTime)) {
  71. throw new RangeError('Invalid interval');
  72. }
  73. var dates = [];
  74. var currentDate = startDate;
  75. currentDate.setHours(0, 0, 0, 0);
  76. var step = options && 'step' in options ? Number(options.step) : 1;
  77. if (step < 1 || isNaN(step)) throw new RangeError('`options.step` must be a number greater than 1');
  78. while (currentDate.getTime() <= endTime) {
  79. dates.push(toDate(currentDate));
  80. currentDate.setDate(currentDate.getDate() + step);
  81. currentDate.setHours(0, 0, 0, 0);
  82. }
  83. return dates;
  84. }