index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = differenceInCalendarISOWeeks;
  6. var _index = _interopRequireDefault(require("../_lib/getTimezoneOffsetInMilliseconds/index.js"));
  7. var _index2 = _interopRequireDefault(require("../startOfISOWeek/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 differenceInCalendarISOWeeks
  13. * @category ISO Week Helpers
  14. * @summary Get the number of calendar ISO weeks between the given dates.
  15. *
  16. * @description
  17. * Get the number of calendar ISO weeks between the given dates.
  18. *
  19. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  20. *
  21. * ### v2.0.0 breaking changes:
  22. *
  23. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  24. *
  25. * @param {Date|Number} dateLeft - the later date
  26. * @param {Date|Number} dateRight - the earlier date
  27. * @returns {Number} the number of calendar ISO weeks
  28. * @throws {TypeError} 2 arguments required
  29. *
  30. * @example
  31. * // How many calendar ISO weeks are between 6 July 2014 and 21 July 2014?
  32. * const result = differenceInCalendarISOWeeks(
  33. * new Date(2014, 6, 21),
  34. * new Date(2014, 6, 6)
  35. * )
  36. * //=> 3
  37. */
  38. function differenceInCalendarISOWeeks(dirtyDateLeft, dirtyDateRight) {
  39. (0, _index3.default)(2, arguments);
  40. var startOfISOWeekLeft = (0, _index2.default)(dirtyDateLeft);
  41. var startOfISOWeekRight = (0, _index2.default)(dirtyDateRight);
  42. var timestampLeft = startOfISOWeekLeft.getTime() - (0, _index.default)(startOfISOWeekLeft);
  43. var timestampRight = startOfISOWeekRight.getTime() - (0, _index.default)(startOfISOWeekRight); // Round the number of days to the nearest integer
  44. // because the number of milliseconds in a week is not constant
  45. // (e.g. it's different in the week of the daylight saving time clock shift)
  46. return Math.round((timestampLeft - timestampRight) / MILLISECONDS_IN_WEEK);
  47. }
  48. module.exports = exports.default;