index.js 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = getISOWeekYear;
  6. var _index = _interopRequireDefault(require("../toDate/index.js"));
  7. var _index2 = _interopRequireDefault(require("../startOfISOWeek/index.js"));
  8. var _index3 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  10. /**
  11. * @name getISOWeekYear
  12. * @category ISO Week-Numbering Year Helpers
  13. * @summary Get the ISO week-numbering year of the given date.
  14. *
  15. * @description
  16. * Get the ISO week-numbering year of the given date,
  17. * which always starts 3 days before the year's first Thursday.
  18. *
  19. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  20. *
  21. * ### v2.0.0 breaking changes:
  22. *
  23. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  24. *
  25. * - The function was renamed from `getISOYear` to `getISOWeekYear`.
  26. * "ISO week year" is short for [ISO week-numbering year](https://en.wikipedia.org/wiki/ISO_week_date).
  27. * This change makes the name consistent with
  28. * locale-dependent week-numbering year helpers, e.g., `getWeekYear`.
  29. *
  30. * @param {Date|Number} date - the given date
  31. * @returns {Number} the ISO week-numbering year
  32. * @throws {TypeError} 1 argument required
  33. *
  34. * @example
  35. * // Which ISO-week numbering year is 2 January 2005?
  36. * const result = getISOWeekYear(new Date(2005, 0, 2))
  37. * //=> 2004
  38. */
  39. function getISOWeekYear(dirtyDate) {
  40. (0, _index3.default)(1, arguments);
  41. var date = (0, _index.default)(dirtyDate);
  42. var year = date.getFullYear();
  43. var fourthOfJanuaryOfNextYear = new Date(0);
  44. fourthOfJanuaryOfNextYear.setFullYear(year + 1, 0, 4);
  45. fourthOfJanuaryOfNextYear.setHours(0, 0, 0, 0);
  46. var startOfNextYear = (0, _index2.default)(fourthOfJanuaryOfNextYear);
  47. var fourthOfJanuaryOfThisYear = new Date(0);
  48. fourthOfJanuaryOfThisYear.setFullYear(year, 0, 4);
  49. fourthOfJanuaryOfThisYear.setHours(0, 0, 0, 0);
  50. var startOfThisYear = (0, _index2.default)(fourthOfJanuaryOfThisYear);
  51. if (date.getTime() >= startOfNextYear.getTime()) {
  52. return year + 1;
  53. } else if (date.getTime() >= startOfThisYear.getTime()) {
  54. return year;
  55. } else {
  56. return year - 1;
  57. }
  58. }
  59. module.exports = exports.default;