index.js 2.6 KB

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