index.js 2.2 KB

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