startOfISOWeekYear.js 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. import { constructFrom } from "./constructFrom.js";
  2. import { getISOWeekYear } from "./getISOWeekYear.js";
  3. import { startOfISOWeek } from "./startOfISOWeek.js";
  4. /**
  5. * The {@link startOfISOWeekYear} function options.
  6. */
  7. /**
  8. * @name startOfISOWeekYear
  9. * @category ISO Week-Numbering Year Helpers
  10. * @summary Return the start of an ISO week-numbering year for the given date.
  11. *
  12. * @description
  13. * Return the start of an ISO week-numbering year,
  14. * which always starts 3 days before the year's first Thursday.
  15. * The result will be in the local timezone.
  16. *
  17. * ISO week-numbering year: http://en.wikipedia.org/wiki/ISO_week_date
  18. *
  19. * @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).
  20. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  21. *
  22. * @param date - The original date
  23. * @param options - An object with options
  24. *
  25. * @returns The start of an ISO week-numbering year
  26. *
  27. * @example
  28. * // The start of an ISO week-numbering year for 2 July 2005:
  29. * const result = startOfISOWeekYear(new Date(2005, 6, 2))
  30. * //=> Mon Jan 03 2005 00:00:00
  31. */
  32. export function startOfISOWeekYear(date, options) {
  33. const year = getISOWeekYear(date, options);
  34. const fourthOfJanuary = constructFrom(options?.in || date, 0);
  35. fourthOfJanuary.setFullYear(year, 0, 4);
  36. fourthOfJanuary.setHours(0, 0, 0, 0);
  37. return startOfISOWeek(fourthOfJanuary);
  38. }
  39. // Fallback for modularized imports:
  40. export default startOfISOWeekYear;