index.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import addWeeks from "../addWeeks/index.js";
  2. import startOfWeek from "../startOfWeek/index.js";
  3. import toDate from "../toDate/index.js";
  4. import requiredArgs from "../_lib/requiredArgs/index.js";
  5. /**
  6. * @name eachWeekOfInterval
  7. * @category Interval Helpers
  8. * @summary Return the array of weeks within the specified time interval.
  9. *
  10. * @description
  11. * Return the array of weeks within the specified time interval.
  12. *
  13. * ### v2.0.0 breaking changes:
  14. *
  15. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  16. *
  17. * @param {Interval} interval - the interval. See [Interval]{@link https://date-fns.org/docs/Interval}
  18. * @param {Object} [options] - an object with options.
  19. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  20. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  21. * @returns {Date[]} the array with starts of weeks from the week of the interval start to the week of the interval end
  22. * @throws {TypeError} 1 argument required
  23. * @throws {RangeError} `options.weekStartsOn` must be 0, 1, ..., 6
  24. * @throws {RangeError} The start of an interval cannot be after its end
  25. * @throws {RangeError} Date in interval cannot be `Invalid Date`
  26. *
  27. * @example
  28. * // Each week within interval 6 October 2014 - 23 November 2014:
  29. * var result = eachWeekOfInterval({
  30. * start: new Date(2014, 9, 6),
  31. * end: new Date(2014, 10, 23)
  32. * })
  33. * //=> [
  34. * // Sun Oct 05 2014 00:00:00,
  35. * // Sun Oct 12 2014 00:00:00,
  36. * // Sun Oct 19 2014 00:00:00,
  37. * // Sun Oct 26 2014 00:00:00,
  38. * // Sun Nov 02 2014 00:00:00,
  39. * // Sun Nov 09 2014 00:00:00,
  40. * // Sun Nov 16 2014 00:00:00,
  41. * // Sun Nov 23 2014 00:00:00
  42. * // ]
  43. */
  44. export default function eachWeekOfInterval(dirtyInterval, options) {
  45. requiredArgs(1, arguments);
  46. var interval = dirtyInterval || {};
  47. var startDate = toDate(interval.start);
  48. var endDate = toDate(interval.end);
  49. var endTime = endDate.getTime(); // Throw an exception if start date is after end date or if any date is `Invalid Date`
  50. if (!(startDate.getTime() <= endTime)) {
  51. throw new RangeError('Invalid interval');
  52. }
  53. var startDateWeek = startOfWeek(startDate, options);
  54. var endDateWeek = startOfWeek(endDate, options); // Some timezones switch DST at midnight, making start of day unreliable in these timezones, 3pm is a safe bet
  55. startDateWeek.setHours(15);
  56. endDateWeek.setHours(15);
  57. endTime = endDateWeek.getTime();
  58. var weeks = [];
  59. var currentWeek = startDateWeek;
  60. while (currentWeek.getTime() <= endTime) {
  61. currentWeek.setHours(0);
  62. weeks.push(toDate(currentWeek));
  63. currentWeek = addWeeks(currentWeek, 1);
  64. currentWeek.setHours(15);
  65. }
  66. return weeks;
  67. }