formatDistanceStrict.d.mts 3.5 KB

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