setWeekYear.d.ts 2.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. import type {
  2. ContextOptions,
  3. DateArg,
  4. FirstWeekContainsDateOptions,
  5. LocalizedOptions,
  6. WeekOptions,
  7. } from "./types.js";
  8. /**
  9. * The {@link setWeekYear} function options.
  10. */
  11. export interface SetWeekYearOptions<DateType extends Date = Date>
  12. extends LocalizedOptions<"options">,
  13. WeekOptions,
  14. FirstWeekContainsDateOptions,
  15. ContextOptions<DateType> {}
  16. /**
  17. * @name setWeekYear
  18. * @category Week-Numbering Year Helpers
  19. * @summary Set the local week-numbering year to the given date.
  20. *
  21. * @description
  22. * Set the local week-numbering year to the given date,
  23. * saving the week number and the weekday number.
  24. * The exact calculation depends on the values of
  25. * `options.weekStartsOn` (which is the index of the first day of the week)
  26. * and `options.firstWeekContainsDate` (which is the day of January, which is always in
  27. * the first week of the week-numbering year)
  28. *
  29. * Week numbering: https://en.wikipedia.org/wiki/Week#The_ISO_week_date_system
  30. *
  31. * @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).
  32. * @typeParam ResultDate - The result `Date` type, it is the type returned from the context function if it is passed, or inferred from the arguments.
  33. *
  34. * @param date - The date to be changed
  35. * @param weekYear - The local week-numbering year of the new date
  36. * @param options - An object with options
  37. *
  38. * @returns The new date with the local week-numbering year set
  39. *
  40. * @example
  41. * // Set the local week-numbering year 2004 to 2 January 2010 with default options:
  42. * const result = setWeekYear(new Date(2010, 0, 2), 2004)
  43. * //=> Sat Jan 03 2004 00:00:00
  44. *
  45. * @example
  46. * // Set the local week-numbering year 2004 to 2 January 2010,
  47. * // if Monday is the first day of week
  48. * // and 4 January is always in the first week of the year:
  49. * const result = setWeekYear(new Date(2010, 0, 2), 2004, {
  50. * weekStartsOn: 1,
  51. * firstWeekContainsDate: 4
  52. * })
  53. * //=> Sat Jan 01 2005 00:00:00
  54. */
  55. export declare function setWeekYear<
  56. DateType extends Date,
  57. ResultDate extends Date = DateType,
  58. >(
  59. date: DateArg<DateType>,
  60. weekYear: number,
  61. options?: SetWeekYearOptions<ResultDate>,
  62. ): ResultDate;