intlFormatDistance.d.ts 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. /**
  2. * The {@link intlFormatDistance} function options.
  3. */
  4. export interface IntlFormatDistanceOptions {
  5. /** Force the distance unit */
  6. unit?: IntlFormatDistanceUnit;
  7. /** The locale(s) to use (see: https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locales_argument) */
  8. locale?:
  9. | Intl.UnicodeBCP47LocaleIdentifier
  10. | Intl.UnicodeBCP47LocaleIdentifier[];
  11. /** The locale matching algorithm to use. Other value: 'lookup'. See MDN for details [Locale identification and negotiation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locale_identification_and_negotiation) */
  12. localeMatcher?: Intl.RelativeTimeFormatLocaleMatcher;
  13. /** The output message format. The values are 'auto' (e.g. `yesterday`), 'always'(e.g. `1 day ago`) */
  14. numeric?: Intl.RelativeTimeFormatNumeric;
  15. /** The length of the result. The values are: 'long' (e.g. `1 month`), 'short' (e.g. 'in 1 mo.'), 'narrow' (e.g. 'in 1 mo.'). The narrow one could be similar to the short one for some locales. */
  16. style?: Intl.RelativeTimeFormatStyle;
  17. }
  18. /**
  19. * The unit used to format the distance in {@link intlFormatDistance}.
  20. */
  21. export type IntlFormatDistanceUnit =
  22. | "year"
  23. | "quarter"
  24. | "month"
  25. | "week"
  26. | "day"
  27. | "hour"
  28. | "minute"
  29. | "second";
  30. /**
  31. * @name intlFormatDistance
  32. * @category Common Helpers
  33. * @summary Formats distance between two dates in a human-readable format
  34. * @description
  35. * The function calculates the difference between two dates and formats it as a human-readable string.
  36. *
  37. * The function will pick the most appropriate unit depending on the distance between dates. For example, if the distance is a few hours, it might return `x hours`. If the distance is a few months, it might return `x months`.
  38. *
  39. * You can also specify a unit to force using it regardless of the distance to get a result like `123456 hours`.
  40. *
  41. * See the table below for the unit picking logic:
  42. *
  43. * | Distance between dates | Result (past) | Result (future) |
  44. * | ---------------------- | -------------- | --------------- |
  45. * | 0 seconds | now | now |
  46. * | 1-59 seconds | X seconds ago | in X seconds |
  47. * | 1-59 minutes | X minutes ago | in X minutes |
  48. * | 1-23 hours | X hours ago | in X hours |
  49. * | 1 day | yesterday | tomorrow |
  50. * | 2-6 days | X days ago | in X days |
  51. * | 7 days | last week | next week |
  52. * | 8 days-1 month | X weeks ago | in X weeks |
  53. * | 1 month | last month | next month |
  54. * | 2-3 months | X months ago | in X months |
  55. * | 1 quarter | last quarter | next quarter |
  56. * | 2-3 quarters | X quarters ago | in X quarters |
  57. * | 1 year | last year | next year |
  58. * | 2+ years | X years ago | in X years |
  59. *
  60. * @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).
  61. *
  62. * @param date - The date
  63. * @param baseDate - The date to compare with.
  64. * @param options - An object with options.
  65. * See MDN for details [Locale identification and negotiation](https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Intl#locale_identification_and_negotiation)
  66. * The narrow one could be similar to the short one for some locales.
  67. *
  68. * @returns The distance in words according to language-sensitive relative time formatting.
  69. *
  70. * @throws `date` must not be Invalid Date
  71. * @throws `baseDate` must not be Invalid Date
  72. * @throws `options.unit` must not be invalid Unit
  73. * @throws `options.locale` must not be invalid locale
  74. * @throws `options.localeMatcher` must not be invalid localeMatcher
  75. * @throws `options.numeric` must not be invalid numeric
  76. * @throws `options.style` must not be invalid style
  77. *
  78. * @example
  79. * // What is the distance between the dates when the fist date is after the second?
  80. * intlFormatDistance(
  81. * new Date(1986, 3, 4, 11, 30, 0),
  82. * new Date(1986, 3, 4, 10, 30, 0)
  83. * )
  84. * //=> 'in 1 hour'
  85. *
  86. * // What is the distance between the dates when the fist date is before the second?
  87. * intlFormatDistance(
  88. * new Date(1986, 3, 4, 10, 30, 0),
  89. * new Date(1986, 3, 4, 11, 30, 0)
  90. * )
  91. * //=> '1 hour ago'
  92. *
  93. * @example
  94. * // Use the unit option to force the function to output the result in quarters. Without setting it, the example would return "next year"
  95. * intlFormatDistance(
  96. * new Date(1987, 6, 4, 10, 30, 0),
  97. * new Date(1986, 3, 4, 10, 30, 0),
  98. * { unit: 'quarter' }
  99. * )
  100. * //=> 'in 5 quarters'
  101. *
  102. * @example
  103. * // Use the locale option to get the result in Spanish. Without setting it, the example would return "in 1 hour".
  104. * intlFormatDistance(
  105. * new Date(1986, 3, 4, 11, 30, 0),
  106. * new Date(1986, 3, 4, 10, 30, 0),
  107. * { locale: 'es' }
  108. * )
  109. * //=> 'dentro de 1 hora'
  110. *
  111. * @example
  112. * // Use the numeric option to force the function to use numeric values. Without setting it, the example would return "tomorrow".
  113. * intlFormatDistance(
  114. * new Date(1986, 3, 5, 11, 30, 0),
  115. * new Date(1986, 3, 4, 11, 30, 0),
  116. * { numeric: 'always' }
  117. * )
  118. * //=> 'in 1 day'
  119. *
  120. * @example
  121. * // Use the style option to force the function to use short values. Without setting it, the example would return "in 2 years".
  122. * intlFormatDistance(
  123. * new Date(1988, 3, 4, 11, 30, 0),
  124. * new Date(1986, 3, 4, 11, 30, 0),
  125. * { style: 'short' }
  126. * )
  127. * //=> 'in 2 yr'
  128. */
  129. export declare function intlFormatDistance<DateType extends Date>(
  130. date: DateType | number | string,
  131. baseDate: DateType | number | string,
  132. options?: IntlFormatDistanceOptions,
  133. ): string;