index.js 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = getWeek;
  6. var _index = _interopRequireDefault(require("../startOfWeek/index.js"));
  7. var _index2 = _interopRequireDefault(require("../startOfWeekYear/index.js"));
  8. var _index3 = _interopRequireDefault(require("../toDate/index.js"));
  9. var _index4 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  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. * ### 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} date - the given date
  31. * @param {Object} [options] - an object with options.
  32. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  33. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  34. * @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
  35. * @returns {Number} the week
  36. * @throws {TypeError} 1 argument required
  37. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  38. * @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7
  39. *
  40. * @example
  41. * // Which week of the local week numbering year is 2 January 2005 with default options?
  42. * const result = getISOWeek(new Date(2005, 0, 2))
  43. * //=> 2
  44. *
  45. * // Which week of the local week numbering year is 2 January 2005,
  46. * // if Monday is the first day of the week,
  47. * // and the first week of the year always contains 4 January?
  48. * const result = getISOWeek(new Date(2005, 0, 2), {
  49. * weekStartsOn: 1,
  50. * firstWeekContainsDate: 4
  51. * })
  52. * //=> 53
  53. */
  54. function getWeek(dirtyDate, options) {
  55. (0, _index4.default)(1, arguments);
  56. var date = (0, _index3.default)(dirtyDate);
  57. var diff = (0, _index.default)(date, options).getTime() - (0, _index2.default)(date, options).getTime(); // Round the number of days to the nearest integer
  58. // because the number of milliseconds in a week is not constant
  59. // (e.g. it's different in the week of the daylight saving time clock shift)
  60. return Math.round(diff / MILLISECONDS_IN_WEEK) + 1;
  61. }
  62. module.exports = exports.default;