index.js 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = getWeekYear;
  6. var _index = _interopRequireDefault(require("../startOfWeek/index.js"));
  7. var _index2 = _interopRequireDefault(require("../toDate/index.js"));
  8. var _index3 = _interopRequireDefault(require("../_lib/toInteger/index.js"));
  9. var _index4 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  10. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  11. /**
  12. * @name getWeekYear
  13. * @category Week-Numbering Year Helpers
  14. * @summary Get the local week-numbering year of the given date.
  15. *
  16. * @description
  17. * Get the local week-numbering year of the given date.
  18. * The exact calculation depends on the values of
  19. * `options.weekStartsOn` (which is the index of the first day of the week)
  20. * and `options.firstWeekContainsDate` (which is the day of January, which is always in
  21. * the first week of the week-numbering year)
  22. *
  23. * Week numbering: https://en.wikipedia.org/wiki/Week#Week_numbering
  24. *
  25. * ### v2.0.0 breaking changes:
  26. *
  27. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  28. *
  29. * @param {Date|Number} date - the given date
  30. * @param {Object} [options] - an object with options.
  31. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  32. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  33. * @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
  34. * @returns {Number} the local week-numbering year
  35. * @throws {TypeError} 1 argument required
  36. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  37. * @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7
  38. *
  39. * @example
  40. * // Which week numbering year is 26 December 2004 with the default settings?
  41. * const result = getWeekYear(new Date(2004, 11, 26))
  42. * //=> 2005
  43. *
  44. * @example
  45. * // Which week numbering year is 26 December 2004 if week starts on Saturday?
  46. * const result = getWeekYear(new Date(2004, 11, 26), { weekStartsOn: 6 })
  47. * //=> 2004
  48. *
  49. * @example
  50. * // Which week numbering year is 26 December 2004 if the first week contains 4 January?
  51. * const result = getWeekYear(new Date(2004, 11, 26), { firstWeekContainsDate: 4 })
  52. * //=> 2004
  53. */
  54. function getWeekYear(dirtyDate, options) {
  55. var _options$locale, _options$locale$optio;
  56. (0, _index4.default)(1, arguments);
  57. var date = (0, _index2.default)(dirtyDate);
  58. var year = date.getFullYear();
  59. var localeFirstWeekContainsDate = options === null || options === void 0 ? void 0 : (_options$locale = options.locale) === null || _options$locale === void 0 ? void 0 : (_options$locale$optio = _options$locale.options) === null || _options$locale$optio === void 0 ? void 0 : _options$locale$optio.firstWeekContainsDate;
  60. var defaultFirstWeekContainsDate = localeFirstWeekContainsDate == null ? 1 : (0, _index3.default)(localeFirstWeekContainsDate);
  61. var firstWeekContainsDate = (options === null || options === void 0 ? void 0 : options.firstWeekContainsDate) == null ? defaultFirstWeekContainsDate : (0, _index3.default)(options.firstWeekContainsDate); // Test if weekStartsOn is between 1 and 7 _and_ is not NaN
  62. if (!(firstWeekContainsDate >= 1 && firstWeekContainsDate <= 7)) {
  63. throw new RangeError('firstWeekContainsDate must be between 1 and 7 inclusively');
  64. }
  65. var firstWeekOfNextYear = new Date(0);
  66. firstWeekOfNextYear.setFullYear(year + 1, 0, firstWeekContainsDate);
  67. firstWeekOfNextYear.setHours(0, 0, 0, 0);
  68. var startOfNextYear = (0, _index.default)(firstWeekOfNextYear, options);
  69. var firstWeekOfThisYear = new Date(0);
  70. firstWeekOfThisYear.setFullYear(year, 0, firstWeekContainsDate);
  71. firstWeekOfThisYear.setHours(0, 0, 0, 0);
  72. var startOfThisYear = (0, _index.default)(firstWeekOfThisYear, options);
  73. if (date.getTime() >= startOfNextYear.getTime()) {
  74. return year + 1;
  75. } else if (date.getTime() >= startOfThisYear.getTime()) {
  76. return year;
  77. } else {
  78. return year - 1;
  79. }
  80. }
  81. module.exports = exports.default;