setWeekYear.js 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. "use strict";
  2. exports.setWeekYear = setWeekYear;
  3. var _index = require("./constructFrom.js");
  4. var _index2 = require("./differenceInCalendarDays.js");
  5. var _index3 = require("./startOfWeekYear.js");
  6. var _index4 = require("./toDate.js");
  7. var _index5 = require("./_lib/defaultOptions.js");
  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. *
  28. * @param date - The date to be changed
  29. * @param weekYear - The local week-numbering year of the new date
  30. * @param options - An object with options
  31. *
  32. * @returns The new date with the local week-numbering year set
  33. *
  34. * @example
  35. * // Set the local week-numbering year 2004 to 2 January 2010 with default options:
  36. * const result = setWeekYear(new Date(2010, 0, 2), 2004)
  37. * //=> Sat Jan 03 2004 00:00:00
  38. *
  39. * @example
  40. * // Set the local week-numbering year 2004 to 2 January 2010,
  41. * // if Monday is the first day of week
  42. * // and 4 January is always in the first week of the year:
  43. * const result = setWeekYear(new Date(2010, 0, 2), 2004, {
  44. * weekStartsOn: 1,
  45. * firstWeekContainsDate: 4
  46. * })
  47. * //=> Sat Jan 01 2005 00:00:00
  48. */
  49. function setWeekYear(date, weekYear, options) {
  50. const defaultOptions = (0, _index5.getDefaultOptions)();
  51. const firstWeekContainsDate =
  52. options?.firstWeekContainsDate ??
  53. options?.locale?.options?.firstWeekContainsDate ??
  54. defaultOptions.firstWeekContainsDate ??
  55. defaultOptions.locale?.options?.firstWeekContainsDate ??
  56. 1;
  57. let _date = (0, _index4.toDate)(date);
  58. const diff = (0, _index2.differenceInCalendarDays)(
  59. _date,
  60. (0, _index3.startOfWeekYear)(_date, options),
  61. );
  62. const firstWeek = (0, _index.constructFrom)(date, 0);
  63. firstWeek.setFullYear(weekYear, 0, firstWeekContainsDate);
  64. firstWeek.setHours(0, 0, 0, 0);
  65. _date = (0, _index3.startOfWeekYear)(firstWeek, options);
  66. _date.setDate(_date.getDate() + diff);
  67. return _date;
  68. }