index.js 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = getISOWeek;
  6. var _index = _interopRequireDefault(require("../toDate/index.js"));
  7. var _index2 = _interopRequireDefault(require("../startOfISOWeek/index.js"));
  8. var _index3 = _interopRequireDefault(require("../startOfISOWeekYear/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 getISOWeek
  14. * @category ISO Week Helpers
  15. * @summary Get the ISO week of the given date.
  16. *
  17. * @description
  18. * Get the ISO week of the given date.
  19. *
  20. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  21. *
  22. * ### v2.0.0 breaking changes:
  23. *
  24. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  25. *
  26. * @param {Date|Number} date - the given date
  27. * @returns {Number} the ISO week
  28. * @throws {TypeError} 1 argument required
  29. *
  30. * @example
  31. * // Which week of the ISO-week numbering year is 2 January 2005?
  32. * const result = getISOWeek(new Date(2005, 0, 2))
  33. * //=> 53
  34. */
  35. function getISOWeek(dirtyDate) {
  36. (0, _index4.default)(1, arguments);
  37. var date = (0, _index.default)(dirtyDate);
  38. var diff = (0, _index2.default)(date).getTime() - (0, _index3.default)(date).getTime(); // Round the number of days 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) + 1;
  42. }
  43. module.exports = exports.default;