index.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = getWeek;
  7. var _index = _interopRequireDefault(require("../startOfWeek/index.js"));
  8. var _index2 = _interopRequireDefault(require("../startOfWeekYear/index.js"));
  9. var _index3 = _interopRequireDefault(require("../toDate/index.js"));
  10. var _index4 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  11. var MILLISECONDS_IN_WEEK = 604800000;
  12. /**
  13. * @name getWeek
  14. * @category Week Helpers
  15. * @summary Get the local week index of the given date.
  16. *
  17. * @description
  18. * Get the local week index of the given date.
  19. * The exact calculation depends on the values of
  20. * `options.weekStartsOn` (which is the index of the first day of the week)
  21. * and `options.firstWeekContainsDate` (which is the day of January, which is always in
  22. * the first week of the week-numbering year)
  23. *
  24. * Week numbering: https://en.wikipedia.org/wiki/Week#Week_numbering
  25. *
  26. * @param {Date|Number} date - the given date
  27. * @param {Object} [options] - an object with options.
  28. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  29. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  30. * @param {1|2|3|4|5|6|7} [options.firstWeekContainsDate=1] - the day of January, which is always in the first week of the year
  31. * @returns {Number} the week
  32. * @throws {TypeError} 1 argument required
  33. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  34. * @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7
  35. *
  36. * @example
  37. * // Which week of the local week numbering year is 2 January 2005 with default options?
  38. * const result = getWeek(new Date(2005, 0, 2))
  39. * //=> 2
  40. *
  41. * // Which week of the local week numbering year is 2 January 2005,
  42. * // if Monday is the first day of the week,
  43. * // and the first week of the year always contains 4 January?
  44. * const result = getWeek(new Date(2005, 0, 2), {
  45. * weekStartsOn: 1,
  46. * firstWeekContainsDate: 4
  47. * })
  48. * //=> 53
  49. */
  50. function getWeek(dirtyDate, options) {
  51. (0, _index4.default)(1, arguments);
  52. var date = (0, _index3.default)(dirtyDate);
  53. var diff = (0, _index.default)(date, options).getTime() - (0, _index2.default)(date, options).getTime();
  54. // Round the number of days to the nearest integer
  55. // because the number of milliseconds in a week is not constant
  56. // (e.g. it's different in the week of the daylight saving time clock shift)
  57. return Math.round(diff / MILLISECONDS_IN_WEEK) + 1;
  58. }
  59. module.exports = exports.default;