intlFormatDistance.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. "use strict";
  2. exports.intlFormatDistance = intlFormatDistance;
  3. var _index = require("./constants.js");
  4. var _index2 = require("./differenceInCalendarDays.js");
  5. var _index3 = require("./differenceInCalendarMonths.js");
  6. var _index4 = require("./differenceInCalendarQuarters.js");
  7. var _index5 = require("./differenceInCalendarWeeks.js");
  8. var _index6 = require("./differenceInCalendarYears.js");
  9. var _index7 = require("./differenceInHours.js");
  10. var _index8 = require("./differenceInMinutes.js");
  11. var _index9 = require("./differenceInSeconds.js");
  12. var _index10 = require("./toDate.js");
  13. /**
  14. * The {@link intlFormatDistance} function options.
  15. */
  16. /**
  17. * The unit used to format the distance in {@link intlFormatDistance}.
  18. */
  19. /**
  20. * @name intlFormatDistance
  21. * @category Common Helpers
  22. * @summary Formats distance between two dates in a human-readable format
  23. * @description
  24. * The function calculates the difference between two dates and formats it as a human-readable string.
  25. *
  26. * 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`.
  27. *
  28. * You can also specify a unit to force using it regardless of the distance to get a result like `123456 hours`.
  29. *
  30. * See the table below for the unit picking logic:
  31. *
  32. * | Distance between dates | Result (past) | Result (future) |
  33. * | ---------------------- | -------------- | --------------- |
  34. * | 0 seconds | now | now |
  35. * | 1-59 seconds | X seconds ago | in X seconds |
  36. * | 1-59 minutes | X minutes ago | in X minutes |
  37. * | 1-23 hours | X hours ago | in X hours |
  38. * | 1 day | yesterday | tomorrow |
  39. * | 2-6 days | X days ago | in X days |
  40. * | 7 days | last week | next week |
  41. * | 8 days-1 month | X weeks ago | in X weeks |
  42. * | 1 month | last month | next month |
  43. * | 2-3 months | X months ago | in X months |
  44. * | 1 quarter | last quarter | next quarter |
  45. * | 2-3 quarters | X quarters ago | in X quarters |
  46. * | 1 year | last year | next year |
  47. * | 2+ years | X years ago | in X years |
  48. *
  49. * @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).
  50. *
  51. * @param date - The date
  52. * @param baseDate - The date to compare with.
  53. * @param options - An object with options.
  54. * 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)
  55. * The narrow one could be similar to the short one for some locales.
  56. *
  57. * @returns The distance in words according to language-sensitive relative time formatting.
  58. *
  59. * @throws `date` must not be Invalid Date
  60. * @throws `baseDate` must not be Invalid Date
  61. * @throws `options.unit` must not be invalid Unit
  62. * @throws `options.locale` must not be invalid locale
  63. * @throws `options.localeMatcher` must not be invalid localeMatcher
  64. * @throws `options.numeric` must not be invalid numeric
  65. * @throws `options.style` must not be invalid style
  66. *
  67. * @example
  68. * // What is the distance between the dates when the fist date is after the second?
  69. * intlFormatDistance(
  70. * new Date(1986, 3, 4, 11, 30, 0),
  71. * new Date(1986, 3, 4, 10, 30, 0)
  72. * )
  73. * //=> 'in 1 hour'
  74. *
  75. * // What is the distance between the dates when the fist date is before the second?
  76. * intlFormatDistance(
  77. * new Date(1986, 3, 4, 10, 30, 0),
  78. * new Date(1986, 3, 4, 11, 30, 0)
  79. * )
  80. * //=> '1 hour ago'
  81. *
  82. * @example
  83. * // Use the unit option to force the function to output the result in quarters. Without setting it, the example would return "next year"
  84. * intlFormatDistance(
  85. * new Date(1987, 6, 4, 10, 30, 0),
  86. * new Date(1986, 3, 4, 10, 30, 0),
  87. * { unit: 'quarter' }
  88. * )
  89. * //=> 'in 5 quarters'
  90. *
  91. * @example
  92. * // Use the locale option to get the result in Spanish. Without setting it, the example would return "in 1 hour".
  93. * intlFormatDistance(
  94. * new Date(1986, 3, 4, 11, 30, 0),
  95. * new Date(1986, 3, 4, 10, 30, 0),
  96. * { locale: 'es' }
  97. * )
  98. * //=> 'dentro de 1 hora'
  99. *
  100. * @example
  101. * // Use the numeric option to force the function to use numeric values. Without setting it, the example would return "tomorrow".
  102. * intlFormatDistance(
  103. * new Date(1986, 3, 5, 11, 30, 0),
  104. * new Date(1986, 3, 4, 11, 30, 0),
  105. * { numeric: 'always' }
  106. * )
  107. * //=> 'in 1 day'
  108. *
  109. * @example
  110. * // Use the style option to force the function to use short values. Without setting it, the example would return "in 2 years".
  111. * intlFormatDistance(
  112. * new Date(1988, 3, 4, 11, 30, 0),
  113. * new Date(1986, 3, 4, 11, 30, 0),
  114. * { style: 'short' }
  115. * )
  116. * //=> 'in 2 yr'
  117. */
  118. function intlFormatDistance(date, baseDate, options) {
  119. let value = 0;
  120. let unit;
  121. const dateLeft = (0, _index10.toDate)(date);
  122. const dateRight = (0, _index10.toDate)(baseDate);
  123. if (!options?.unit) {
  124. // Get the unit based on diffInSeconds calculations if no unit is specified
  125. const diffInSeconds = (0, _index9.differenceInSeconds)(dateLeft, dateRight); // The smallest unit
  126. if (Math.abs(diffInSeconds) < _index.secondsInMinute) {
  127. value = (0, _index9.differenceInSeconds)(dateLeft, dateRight);
  128. unit = "second";
  129. } else if (Math.abs(diffInSeconds) < _index.secondsInHour) {
  130. value = (0, _index8.differenceInMinutes)(dateLeft, dateRight);
  131. unit = "minute";
  132. } else if (
  133. Math.abs(diffInSeconds) < _index.secondsInDay &&
  134. Math.abs((0, _index2.differenceInCalendarDays)(dateLeft, dateRight)) < 1
  135. ) {
  136. value = (0, _index7.differenceInHours)(dateLeft, dateRight);
  137. unit = "hour";
  138. } else if (
  139. Math.abs(diffInSeconds) < _index.secondsInWeek &&
  140. (value = (0, _index2.differenceInCalendarDays)(dateLeft, dateRight)) &&
  141. Math.abs(value) < 7
  142. ) {
  143. unit = "day";
  144. } else if (Math.abs(diffInSeconds) < _index.secondsInMonth) {
  145. value = (0, _index5.differenceInCalendarWeeks)(dateLeft, dateRight);
  146. unit = "week";
  147. } else if (Math.abs(diffInSeconds) < _index.secondsInQuarter) {
  148. value = (0, _index3.differenceInCalendarMonths)(dateLeft, dateRight);
  149. unit = "month";
  150. } else if (Math.abs(diffInSeconds) < _index.secondsInYear) {
  151. if ((0, _index4.differenceInCalendarQuarters)(dateLeft, dateRight) < 4) {
  152. // To filter out cases that are less than a year but match 4 quarters
  153. value = (0, _index4.differenceInCalendarQuarters)(dateLeft, dateRight);
  154. unit = "quarter";
  155. } else {
  156. value = (0, _index6.differenceInCalendarYears)(dateLeft, dateRight);
  157. unit = "year";
  158. }
  159. } else {
  160. value = (0, _index6.differenceInCalendarYears)(dateLeft, dateRight);
  161. unit = "year";
  162. }
  163. } else {
  164. // Get the value if unit is specified
  165. unit = options?.unit;
  166. if (unit === "second") {
  167. value = (0, _index9.differenceInSeconds)(dateLeft, dateRight);
  168. } else if (unit === "minute") {
  169. value = (0, _index8.differenceInMinutes)(dateLeft, dateRight);
  170. } else if (unit === "hour") {
  171. value = (0, _index7.differenceInHours)(dateLeft, dateRight);
  172. } else if (unit === "day") {
  173. value = (0, _index2.differenceInCalendarDays)(dateLeft, dateRight);
  174. } else if (unit === "week") {
  175. value = (0, _index5.differenceInCalendarWeeks)(dateLeft, dateRight);
  176. } else if (unit === "month") {
  177. value = (0, _index3.differenceInCalendarMonths)(dateLeft, dateRight);
  178. } else if (unit === "quarter") {
  179. value = (0, _index4.differenceInCalendarQuarters)(dateLeft, dateRight);
  180. } else if (unit === "year") {
  181. value = (0, _index6.differenceInCalendarYears)(dateLeft, dateRight);
  182. }
  183. }
  184. const rtf = new Intl.RelativeTimeFormat(options?.locale, {
  185. localeMatcher: options?.localeMatcher,
  186. numeric: options?.numeric || "auto",
  187. style: options?.style,
  188. });
  189. return rtf.format(value, unit);
  190. }