UNPKG

1.21 kBJavaScriptView Raw
1import { millisecondsInWeek } from "./constants.js";
2import { startOfISOWeek } from "./startOfISOWeek.js";
3import { startOfISOWeekYear } from "./startOfISOWeekYear.js";
4import { toDate } from "./toDate.js";
5
6/**
7 * The {@link getISOWeek} function options.
8 */
9
10/**
11 * @name getISOWeek
12 * @category ISO Week Helpers
13 * @summary Get the ISO week of the given date.
14 *
15 * @description
16 * Get the ISO week of the given date.
17 *
18 * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
19 *
20 * @param date - The given date
21 * @param options - The options
22 *
23 * @returns The ISO week
24 *
25 * @example
26 * // Which week of the ISO-week numbering year is 2 January 2005?
27 * const result = getISOWeek(new Date(2005, 0, 2))
28 * //=> 53
29 */
30export function getISOWeek(date, options) {
31 const _date = toDate(date, options?.in);
32 const diff = +startOfISOWeek(_date) - +startOfISOWeekYear(_date);
33
34 // Round the number of weeks to the nearest integer because the number of
35 // milliseconds in a week is not constant (e.g. it's different in the week of
36 // the daylight saving time clock shift).
37 return Math.round(diff / millisecondsInWeek) + 1;
38}
39
40// Fallback for modularized imports:
41export default getISOWeek;