startOfISOWeekYear.mjs 1.3 KB

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