setWeekYear.cjs 2.8 KB

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