setWeekYear.mjs 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869
  1. import { constructFrom } from "./constructFrom.mjs";
  2. import { differenceInCalendarDays } from "./differenceInCalendarDays.mjs";
  3. import { startOfWeekYear } from "./startOfWeekYear.mjs";
  4. import { toDate } from "./toDate.mjs";
  5. import { getDefaultOptions } from "./_lib/defaultOptions.mjs";
  6. /**
  7. * The {@link setWeekYear} function options.
  8. */
  9. /**
  10. * @name setWeekYear
  11. * @category Week-Numbering Year Helpers
  12. * @summary Set the local week-numbering year to the given date.
  13. *
  14. * @description
  15. * Set the local week-numbering year to the given date,
  16. * saving the week number and the weekday number.
  17. * The exact calculation depends on the values of
  18. * `options.weekStartsOn` (which is the index of the first day of the week)
  19. * and `options.firstWeekContainsDate` (which is the day of January, which is always in
  20. * the first week of the week-numbering year)
  21. *
  22. * Week numbering: https://en.wikipedia.org/wiki/Week#The_ISO_week_date_system
  23. *
  24. * @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).
  25. *
  26. * @param date - The date to be changed
  27. * @param weekYear - The local week-numbering year of the new date
  28. * @param options - An object with options
  29. *
  30. * @returns The new date with the local week-numbering year set
  31. *
  32. * @example
  33. * // Set the local week-numbering year 2004 to 2 January 2010 with default options:
  34. * const result = setWeekYear(new Date(2010, 0, 2), 2004)
  35. * //=> Sat Jan 03 2004 00:00:00
  36. *
  37. * @example
  38. * // Set the local week-numbering year 2004 to 2 January 2010,
  39. * // if Monday is the first day of week
  40. * // and 4 January is always in the first week of the year:
  41. * const result = setWeekYear(new Date(2010, 0, 2), 2004, {
  42. * weekStartsOn: 1,
  43. * firstWeekContainsDate: 4
  44. * })
  45. * //=> Sat Jan 01 2005 00:00:00
  46. */
  47. export function setWeekYear(date, weekYear, options) {
  48. const defaultOptions = getDefaultOptions();
  49. const firstWeekContainsDate =
  50. options?.firstWeekContainsDate ??
  51. options?.locale?.options?.firstWeekContainsDate ??
  52. defaultOptions.firstWeekContainsDate ??
  53. defaultOptions.locale?.options?.firstWeekContainsDate ??
  54. 1;
  55. let _date = toDate(date);
  56. const diff = differenceInCalendarDays(_date, startOfWeekYear(_date, options));
  57. const firstWeek = constructFrom(date, 0);
  58. firstWeek.setFullYear(weekYear, 0, firstWeekContainsDate);
  59. firstWeek.setHours(0, 0, 0, 0);
  60. _date = startOfWeekYear(firstWeek, options);
  61. _date.setDate(_date.getDate() + diff);
  62. return _date;
  63. }
  64. // Fallback for modularized imports:
  65. export default setWeekYear;