formatDistanceStrict.d.ts 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  1. import type {
  2. ContextOptions,
  3. DateArg,
  4. LocalizedOptions,
  5. RoundingOptions,
  6. } from "./types.js";
  7. /**
  8. * The {@link formatDistanceStrict} function options.
  9. */
  10. export interface FormatDistanceStrictOptions
  11. extends LocalizedOptions<"formatDistance">,
  12. RoundingOptions,
  13. ContextOptions<Date> {
  14. /** Add "X ago"/"in X" in the locale language */
  15. addSuffix?: boolean;
  16. /** If specified, will force the unit */
  17. unit?: FormatDistanceStrictUnit;
  18. }
  19. /**
  20. * The unit used to format the distance in {@link formatDistanceStrict}.
  21. */
  22. export type FormatDistanceStrictUnit =
  23. | "second"
  24. | "minute"
  25. | "hour"
  26. | "day"
  27. | "month"
  28. | "year";
  29. /**
  30. * @name formatDistanceStrict
  31. * @category Common Helpers
  32. * @summary Return the distance between the given dates in words.
  33. *
  34. * @description
  35. * Return the distance between the given dates in words, using strict units.
  36. * This is like `formatDistance`, but does not use helpers like 'almost', 'over',
  37. * 'less than' and the like.
  38. *
  39. * | Distance between dates | Result |
  40. * |------------------------|---------------------|
  41. * | 0 ... 59 secs | [0..59] seconds |
  42. * | 1 ... 59 mins | [1..59] minutes |
  43. * | 1 ... 23 hrs | [1..23] hours |
  44. * | 1 ... 29 days | [1..29] days |
  45. * | 1 ... 11 months | [1..11] months |
  46. * | 1 ... N years | [1..N] years |
  47. *
  48. * @param laterDate - The date
  49. * @param earlierDate - The date to compare with
  50. * @param options - An object with options
  51. *
  52. * @returns The distance in words
  53. *
  54. * @throws `date` must not be Invalid Date
  55. * @throws `baseDate` must not be Invalid Date
  56. * @throws `options.unit` must be 'second', 'minute', 'hour', 'day', 'month' or 'year'
  57. * @throws `options.locale` must contain `formatDistance` property
  58. *
  59. * @example
  60. * // What is the distance between 2 July 2014 and 1 January 2015?
  61. * const result = formatDistanceStrict(new Date(2014, 6, 2), new Date(2015, 0, 2))
  62. * //=> '6 months'
  63. *
  64. * @example
  65. * // What is the distance between 1 January 2015 00:00:15
  66. * // and 1 January 2015 00:00:00?
  67. * const result = formatDistanceStrict(
  68. * new Date(2015, 0, 1, 0, 0, 15),
  69. * new Date(2015, 0, 1, 0, 0, 0)
  70. * )
  71. * //=> '15 seconds'
  72. *
  73. * @example
  74. * // What is the distance from 1 January 2016
  75. * // to 1 January 2015, with a suffix?
  76. * const result = formatDistanceStrict(new Date(2015, 0, 1), new Date(2016, 0, 1), {
  77. * addSuffix: true
  78. * })
  79. * //=> '1 year ago'
  80. *
  81. * @example
  82. * // What is the distance from 1 January 2016
  83. * // to 1 January 2015, in minutes?
  84. * const result = formatDistanceStrict(new Date(2016, 0, 1), new Date(2015, 0, 1), {
  85. * unit: 'minute'
  86. * })
  87. * //=> '525600 minutes'
  88. *
  89. * @example
  90. * // What is the distance from 1 January 2015
  91. * // to 28 January 2015, in months, rounded up?
  92. * const result = formatDistanceStrict(new Date(2015, 0, 28), new Date(2015, 0, 1), {
  93. * unit: 'month',
  94. * roundingMethod: 'ceil'
  95. * })
  96. * //=> '1 month'
  97. *
  98. * @example
  99. * // What is the distance between 1 August 2016 and 1 January 2015 in Esperanto?
  100. * import { eoLocale } from 'date-fns/locale/eo'
  101. * const result = formatDistanceStrict(new Date(2016, 7, 1), new Date(2015, 0, 1), {
  102. * locale: eoLocale
  103. * })
  104. * //=> '1 jaro'
  105. */
  106. export declare function formatDistanceStrict(
  107. laterDate: DateArg<Date> & {},
  108. earlierDate: DateArg<Date> & {},
  109. options?: FormatDistanceStrictOptions,
  110. ): string;