index.js 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. import differenceInCalendarDays from "../differenceInCalendarDays/index.js";
  2. import startOfWeekYear from "../startOfWeekYear/index.js";
  3. import toDate from "../toDate/index.js";
  4. import toInteger from "../_lib/toInteger/index.js";
  5. import requiredArgs from "../_lib/requiredArgs/index.js";
  6. /**
  7. * @name setWeekYear
  8. * @category Week-Numbering Year Helpers
  9. * @summary Set the local week-numbering year to the given date.
  10. *
  11. * @description
  12. * Set the local week-numbering year to the given date,
  13. * saving the week number and the weekday number.
  14. * The exact calculation depends on the values of
  15. * `options.weekStartsOn` (which is the index of the first day of the week)
  16. * and `options.firstWeekContainsDate` (which is the day of January, which is always in
  17. * the first week of the week-numbering year)
  18. *
  19. * Week numbering: https://en.wikipedia.org/wiki/Week#Week_numbering
  20. *
  21. * ### v2.0.0 breaking changes:
  22. *
  23. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  24. *
  25. * @param {Date|Number} date - the date to be changed
  26. * @param {Number} weekYear - the local week-numbering year of the new date
  27. * @param {Object} [options] - an object with options.
  28. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  29. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  30. * @param {1|2|3|4|5|6|7} [options.firstWeekContainsDate=1] - the day of January, which is always in the first week of the year
  31. * @returns {Date} the new date with the local week-numbering year set
  32. * @throws {TypeError} 2 arguments required
  33. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  34. * @throws {RangeError} `options.firstWeekContainsDate` must be between 1 and 7
  35. *
  36. * @example
  37. * // Set the local week-numbering year 2004 to 2 January 2010 with default options:
  38. * var result = setWeekYear(new Date(2010, 0, 2), 2004)
  39. * //=> Sat Jan 03 2004 00:00:00
  40. *
  41. * @example
  42. * // Set the local week-numbering year 2004 to 2 January 2010,
  43. * // if Monday is the first day of week
  44. * // and 4 January is always in the first week of the year:
  45. * var result = setWeekYear(new Date(2010, 0, 2), 2004, {
  46. * weekStartsOn: 1,
  47. * firstWeekContainsDate: 4
  48. * })
  49. * //=> Sat Jan 01 2005 00:00:00
  50. */
  51. export default function setWeekYear(dirtyDate, dirtyWeekYear, dirtyOptions) {
  52. requiredArgs(2, arguments);
  53. var options = dirtyOptions || {};
  54. var locale = options.locale;
  55. var localeFirstWeekContainsDate = locale && locale.options && locale.options.firstWeekContainsDate;
  56. var defaultFirstWeekContainsDate = localeFirstWeekContainsDate == null ? 1 : toInteger(localeFirstWeekContainsDate);
  57. var firstWeekContainsDate = options.firstWeekContainsDate == null ? defaultFirstWeekContainsDate : toInteger(options.firstWeekContainsDate);
  58. var date = toDate(dirtyDate);
  59. var weekYear = toInteger(dirtyWeekYear);
  60. var diff = differenceInCalendarDays(date, startOfWeekYear(date, dirtyOptions));
  61. var firstWeek = new Date(0);
  62. firstWeek.setFullYear(weekYear, 0, firstWeekContainsDate);
  63. firstWeek.setHours(0, 0, 0, 0);
  64. date = startOfWeekYear(firstWeek, dirtyOptions);
  65. date.setDate(date.getDate() + diff);
  66. return date;
  67. }