index.js 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. "use strict";
  2. var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault").default;
  3. Object.defineProperty(exports, "__esModule", {
  4. value: true
  5. });
  6. exports.default = getISOWeeksInYear;
  7. var _index = _interopRequireDefault(require("../startOfISOWeekYear/index.js"));
  8. var _index2 = _interopRequireDefault(require("../addWeeks/index.js"));
  9. var _index3 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  10. var MILLISECONDS_IN_WEEK = 604800000;
  11. /**
  12. * @name getISOWeeksInYear
  13. * @category ISO Week-Numbering Year Helpers
  14. * @summary Get the number of weeks in an ISO week-numbering year of the given date.
  15. *
  16. * @description
  17. * Get the number of weeks in an ISO week-numbering year of the given date.
  18. *
  19. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  20. *
  21. * @param {Date|Number} date - the given date
  22. * @returns {Number} the number of ISO weeks in a year
  23. * @throws {TypeError} 1 argument required
  24. *
  25. * @example
  26. * // How many weeks are in ISO week-numbering year 2015?
  27. * const result = getISOWeeksInYear(new Date(2015, 1, 11))
  28. * //=> 53
  29. */
  30. function getISOWeeksInYear(dirtyDate) {
  31. (0, _index3.default)(1, arguments);
  32. var thisYear = (0, _index.default)(dirtyDate);
  33. var nextYear = (0, _index.default)((0, _index2.default)(thisYear, 60));
  34. var diff = nextYear.valueOf() - thisYear.valueOf();
  35. // Round the number of weeks to the nearest integer
  36. // because the number of milliseconds in a week is not constant
  37. // (e.g. it's different in the week of the daylight saving time clock shift)
  38. return Math.round(diff / MILLISECONDS_IN_WEEK);
  39. }
  40. module.exports = exports.default;