setWeekYear.js 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import { getDefaultOptions } from "./_lib/defaultOptions.js";
  2. import { constructFrom } from "./constructFrom.js";
  3. import { differenceInCalendarDays } from "./differenceInCalendarDays.js";
  4. import { startOfWeekYear } from "./startOfWeekYear.js";
  5. import { toDate } from "./toDate.js";
  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. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  26. *
  27. * @param date - The date to be changed
  28. * @param weekYear - The local week-numbering year of the new date
  29. * @param options - An object with options
  30. *
  31. * @returns The new date with the local week-numbering year set
  32. *
  33. * @example
  34. * // Set the local week-numbering year 2004 to 2 January 2010 with default options:
  35. * const result = setWeekYear(new Date(2010, 0, 2), 2004)
  36. * //=> Sat Jan 03 2004 00:00:00
  37. *
  38. * @example
  39. * // Set the local week-numbering year 2004 to 2 January 2010,
  40. * // if Monday is the first day of week
  41. * // and 4 January is always in the first week of the year:
  42. * const result = setWeekYear(new Date(2010, 0, 2), 2004, {
  43. * weekStartsOn: 1,
  44. * firstWeekContainsDate: 4
  45. * })
  46. * //=> Sat Jan 01 2005 00:00:00
  47. */
  48. export function setWeekYear(date, weekYear, options) {
  49. const defaultOptions = getDefaultOptions();
  50. const firstWeekContainsDate =
  51. options?.firstWeekContainsDate ??
  52. options?.locale?.options?.firstWeekContainsDate ??
  53. defaultOptions.firstWeekContainsDate ??
  54. defaultOptions.locale?.options?.firstWeekContainsDate ??
  55. 1;
  56. const diff = differenceInCalendarDays(
  57. toDate(date, options?.in),
  58. startOfWeekYear(date, options),
  59. options,
  60. );
  61. const firstWeek = constructFrom(options?.in || date, 0);
  62. firstWeek.setFullYear(weekYear, 0, firstWeekContainsDate);
  63. firstWeek.setHours(0, 0, 0, 0);
  64. const date_ = startOfWeekYear(firstWeek, options);
  65. date_.setDate(date_.getDate() + diff);
  66. return date_;
  67. }
  68. // Fallback for modularized imports:
  69. export default setWeekYear;