formatRelative.d.ts 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950
  1. import type { LocalizedOptions, WeekOptions } from "./types.js";
  2. /**
  3. * The {@link formatRelative} function options.
  4. */
  5. export interface FormatRelativeOptions
  6. extends LocalizedOptions<
  7. "options" | "localize" | "formatLong" | "formatRelative"
  8. >,
  9. WeekOptions {}
  10. /**
  11. * @name formatRelative
  12. * @category Common Helpers
  13. * @summary Represent the date in words relative to the given base date.
  14. *
  15. * @description
  16. * Represent the date in words relative to the given base date.
  17. *
  18. * | Distance to the base date | Result |
  19. * |---------------------------|---------------------------|
  20. * | Previous 6 days | last Sunday at 04:30 AM |
  21. * | Last day | yesterday at 04:30 AM |
  22. * | Same day | today at 04:30 AM |
  23. * | Next day | tomorrow at 04:30 AM |
  24. * | Next 6 days | Sunday at 04:30 AM |
  25. * | Other | 12/31/2017 |
  26. *
  27. * @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).
  28. *
  29. * @param date - The date to format
  30. * @param baseDate - The date to compare with
  31. * @param options - An object with options
  32. *
  33. * @returns The date in words
  34. *
  35. * @throws `date` must not be Invalid Date
  36. * @throws `baseDate` must not be Invalid Date
  37. * @throws `options.locale` must contain `localize` property
  38. * @throws `options.locale` must contain `formatLong` property
  39. * @throws `options.locale` must contain `formatRelative` property
  40. *
  41. * @example
  42. * // Represent the date of 6 days ago in words relative to the given base date. In this example, today is Wednesday
  43. * const result = formatRelative(subDays(new Date(), 6), new Date())
  44. * //=> "last Thursday at 12:45 AM"
  45. */
  46. export declare function formatRelative<DateType extends Date>(
  47. date: DateType | number | string,
  48. baseDate: DateType | number | string,
  49. options?: FormatRelativeOptions,
  50. ): string;