index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = differenceInCalendarWeeks;
  6. var _index = _interopRequireDefault(require("../startOfWeek/index.js"));
  7. var _index2 = _interopRequireDefault(require("../_lib/getTimezoneOffsetInMilliseconds/index.js"));
  8. var _index3 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. var MILLISECONDS_IN_WEEK = 604800000;
  11. /**
  12. * @name differenceInCalendarWeeks
  13. * @category Week Helpers
  14. * @summary Get the number of calendar weeks between the given dates.
  15. *
  16. * @description
  17. * Get the number of calendar weeks between the given dates.
  18. *
  19. * ### v2.0.0 breaking changes:
  20. *
  21. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  22. *
  23. * @param {Date|Number} dateLeft - the later date
  24. * @param {Date|Number} dateRight - the earlier date
  25. * @param {Object} [options] - an object with options.
  26. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  27. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  28. * @returns {Number} the number of calendar weeks
  29. * @throws {TypeError} 2 arguments required
  30. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  31. *
  32. * @example
  33. * // How many calendar weeks are between 5 July 2014 and 20 July 2014?
  34. * const result = differenceInCalendarWeeks(
  35. * new Date(2014, 6, 20),
  36. * new Date(2014, 6, 5)
  37. * )
  38. * //=> 3
  39. *
  40. * @example
  41. * // If the week starts on Monday,
  42. * // how many calendar weeks are between 5 July 2014 and 20 July 2014?
  43. * const result = differenceInCalendarWeeks(
  44. * new Date(2014, 6, 20),
  45. * new Date(2014, 6, 5),
  46. * { weekStartsOn: 1 }
  47. * )
  48. * //=> 2
  49. */
  50. function differenceInCalendarWeeks(dirtyDateLeft, dirtyDateRight, dirtyOptions) {
  51. (0, _index3.default)(2, arguments);
  52. var startOfWeekLeft = (0, _index.default)(dirtyDateLeft, dirtyOptions);
  53. var startOfWeekRight = (0, _index.default)(dirtyDateRight, dirtyOptions);
  54. var timestampLeft = startOfWeekLeft.getTime() - (0, _index2.default)(startOfWeekLeft);
  55. var timestampRight = startOfWeekRight.getTime() - (0, _index2.default)(startOfWeekRight); // Round the number of days to the nearest integer
  56. // because the number of milliseconds in a week is not constant
  57. // (e.g. it's different in the week of the daylight saving time clock shift)
  58. return Math.round((timestampLeft - timestampRight) / MILLISECONDS_IN_WEEK);
  59. }
  60. module.exports = exports.default;