UNPKG

1.43 kBJavaScriptView Raw
1import { getISOWeekYear } from "./getISOWeekYear.mjs";
2import { startOfISOWeek } from "./startOfISOWeek.mjs";
3import { constructFrom } from "./constructFrom.mjs";
4
5/**
6 * @name lastDayOfISOWeekYear
7 * @category ISO Week-Numbering Year Helpers
8 * @summary Return the last day of an ISO week-numbering year for the given date.
9 *
10 * @description
11 * Return the last day of an ISO week-numbering year,
12 * which always starts 3 days before the year's first Thursday.
13 * The result will be in the local timezone.
14 *
15 * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
16 *
17 * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
18 *
19 * @param date - The original date
20 *
21 * @returns The end of an ISO week-numbering year
22 *
23 * @example
24 * // The last day of an ISO week-numbering year for 2 July 2005:
25 * const result = lastDayOfISOWeekYear(new Date(2005, 6, 2))
26 * //=> Sun Jan 01 2006 00:00:00
27 */
28export function lastDayOfISOWeekYear(date) {
29 const year = getISOWeekYear(date);
30 const fourthOfJanuary = constructFrom(date, 0);
31 fourthOfJanuary.setFullYear(year + 1, 0, 4);
32 fourthOfJanuary.setHours(0, 0, 0, 0);
33 const _date = startOfISOWeek(fourthOfJanuary);
34 _date.setDate(_date.getDate() - 1);
35 return _date;
36}
37
38// Fallback for modularized imports:
39export default lastDayOfISOWeekYear;