1 | ;
|
2 |
|
3 | Object.defineProperty(exports, "__esModule", {
|
4 | value: true
|
5 | });
|
6 | exports.default = getWeek;
|
7 |
|
8 | var _index = _interopRequireDefault(require("../startOfWeek/index.js"));
|
9 |
|
10 | var _index2 = _interopRequireDefault(require("../startOfWeekYear/index.js"));
|
11 |
|
12 | var _index3 = _interopRequireDefault(require("../toDate/index.js"));
|
13 |
|
14 | var _index4 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
|
15 |
|
16 | function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
|
17 |
|
18 | var MILLISECONDS_IN_WEEK = 604800000;
|
19 | /**
|
20 | * @name getWeek
|
21 | * @category Week Helpers
|
22 | * @summary Get the local week index of the given date.
|
23 | *
|
24 | * @description
|
25 | * Get the local week index of the given date.
|
26 | * The exact calculation depends on the values of
|
27 | * `options.weekStartsOn` (which is the index of the first day of the week)
|
28 | * and `options.firstWeekContainsDate` (which is the day of January, which is always in
|
29 | * the first week of the week-numbering year)
|
30 | *
|
31 | * Week numbering: https://en.wikipedia.org/wiki/Week#Week_numbering
|
32 | *
|
33 | * ### v2.0.0 breaking changes:
|
34 | *
|
35 | * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
|
36 | *
|
37 | * @param {Date|Number} date - the given date
|
38 | * @param {Object} [options] - an object with options.
|
39 | * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
|
40 | * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
|
41 | * @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
|
42 | * @returns {Number} the week
|
43 | * @throws {TypeError} 1 argument required
|
44 | * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
|
45 | * @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7
|
46 | *
|
47 | * @example
|
48 | * // Which week of the local week numbering year is 2 January 2005 with default options?
|
49 | * var result = getISOWeek(new Date(2005, 0, 2))
|
50 | * //=> 2
|
51 | *
|
52 | * // Which week of the local week numbering year is 2 January 2005,
|
53 | * // if Monday is the first day of the week,
|
54 | * // and the first week of the year always contains 4 January?
|
55 | * var result = getISOWeek(new Date(2005, 0, 2), {
|
56 | * weekStartsOn: 1,
|
57 | * firstWeekContainsDate: 4
|
58 | * })
|
59 | * //=> 53
|
60 | */
|
61 |
|
62 | function getWeek(dirtyDate, options) {
|
63 | (0, _index4.default)(1, arguments);
|
64 | var date = (0, _index3.default)(dirtyDate);
|
65 | var diff = (0, _index.default)(date, options).getTime() - (0, _index2.default)(date, options).getTime(); // Round the number of days to the nearest integer
|
66 | // because the number of milliseconds in a week is not constant
|
67 | // (e.g. it's different in the week of the daylight saving time clock shift)
|
68 |
|
69 | return Math.round(diff / MILLISECONDS_IN_WEEK) + 1;
|
70 | }
|
71 |
|
72 | module.exports = exports.default; |
\ | No newline at end of file |