index.js 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = differenceInWeeks;
  6. var _index = _interopRequireDefault(require("../differenceInDays/index.js"));
  7. var _index2 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * @name differenceInWeeks
  11. * @category Week Helpers
  12. * @summary Get the number of full weeks between the given dates.
  13. *
  14. * @description
  15. * Get the number of full weeks between two dates. Fractional weeks are
  16. * truncated towards zero.
  17. *
  18. * One "full week" is the distance between a local time in one day to the same
  19. * local time 7 days earlier or later. A full week can sometimes be less than
  20. * or more than 7*24 hours if a daylight savings change happens between two dates.
  21. *
  22. * To ignore DST and only measure exact 7*24-hour periods, use this instead:
  23. * `Math.floor(differenceInHours(dateLeft, dateRight)/(7*24))|0`.
  24. *
  25. *
  26. * ### v2.0.0 breaking changes:
  27. *
  28. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  29. *
  30. * @param {Date|Number} dateLeft - the later date
  31. * @param {Date|Number} dateRight - the earlier date
  32. * @returns {Number} the number of full weeks
  33. * @throws {TypeError} 2 arguments required
  34. *
  35. * @example
  36. * // How many full weeks are between 5 July 2014 and 20 July 2014?
  37. * const result = differenceInWeeks(new Date(2014, 6, 20), new Date(2014, 6, 5))
  38. * //=> 2
  39. *
  40. * // How many full weeks are between
  41. * // 1 March 2020 0:00 and 6 June 2020 0:00 ?
  42. * // Note: because local time is used, the
  43. * // result will always be 8 weeks (54 days),
  44. * // even if DST starts and the period has
  45. * // only 54*24-1 hours.
  46. * const result = differenceInWeeks(
  47. * new Date(2020, 5, 1),
  48. * new Date(2020, 2, 6)
  49. * )
  50. * //=> 8
  51. */
  52. function differenceInWeeks(dirtyDateLeft, dirtyDateRight) {
  53. (0, _index2.default)(2, arguments);
  54. var diff = (0, _index.default)(dirtyDateLeft, dirtyDateRight) / 7;
  55. return diff > 0 ? Math.floor(diff) : Math.ceil(diff);
  56. }
  57. module.exports = exports.default;