index.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = getISOWeeksInYear;
  6. var _index = _interopRequireDefault(require("../startOfISOWeekYear/index.js"));
  7. var _index2 = _interopRequireDefault(require("../addWeeks/index.js"));
  8. var _index3 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  9. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  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. * ### 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. * @param {Date|Number} date - the given date
  26. * @returns {Number} the number of ISO weeks in a year
  27. * @throws {TypeError} 1 argument required
  28. *
  29. * @example
  30. * // How many weeks are in ISO week-numbering year 2015?
  31. * const result = getISOWeeksInYear(new Date(2015, 1, 11))
  32. * //=> 53
  33. */
  34. function getISOWeeksInYear(dirtyDate) {
  35. (0, _index3.default)(1, arguments);
  36. var thisYear = (0, _index.default)(dirtyDate);
  37. var nextYear = (0, _index.default)((0, _index2.default)(thisYear, 60));
  38. var diff = nextYear.valueOf() - thisYear.valueOf(); // Round the number of weeks to the nearest integer
  39. // because the number of milliseconds in a week is not constant
  40. // (e.g. it's different in the week of the daylight saving time clock shift)
  41. return Math.round(diff / MILLISECONDS_IN_WEEK);
  42. }
  43. module.exports = exports.default;