setWeek.d.cts 2.0 KB

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