intlFormatDistance.cjs 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. "use strict";
  2. exports.intlFormatDistance = intlFormatDistance;
  3. var _index = require("./_lib/normalizeDates.cjs");
  4. var _index2 = require("./constants.cjs");
  5. var _index3 = require("./differenceInCalendarDays.cjs");
  6. var _index4 = require("./differenceInCalendarMonths.cjs");
  7. var _index5 = require("./differenceInCalendarQuarters.cjs");
  8. var _index6 = require("./differenceInCalendarWeeks.cjs");
  9. var _index7 = require("./differenceInCalendarYears.cjs");
  10. var _index8 = require("./differenceInHours.cjs");
  11. var _index9 = require("./differenceInMinutes.cjs");
  12. var _index10 = require("./differenceInSeconds.cjs");
  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. * @param laterDate - The date
  50. * @param earlierDate - The date to compare with.
  51. * @param options - An object with options.
  52. * 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)
  53. * The narrow one could be similar to the short one for some locales.
  54. *
  55. * @returns The distance in words according to language-sensitive relative time formatting.
  56. *
  57. * @throws `date` must not be Invalid Date
  58. * @throws `baseDate` must not be Invalid Date
  59. * @throws `options.unit` must not be invalid Unit
  60. * @throws `options.locale` must not be invalid locale
  61. * @throws `options.localeMatcher` must not be invalid localeMatcher
  62. * @throws `options.numeric` must not be invalid numeric
  63. * @throws `options.style` must not be invalid style
  64. *
  65. * @example
  66. * // What is the distance between the dates when the fist date is after the second?
  67. * intlFormatDistance(
  68. * new Date(1986, 3, 4, 11, 30, 0),
  69. * new Date(1986, 3, 4, 10, 30, 0)
  70. * )
  71. * //=> 'in 1 hour'
  72. *
  73. * // What is the distance between the dates when the fist date is before the second?
  74. * intlFormatDistance(
  75. * new Date(1986, 3, 4, 10, 30, 0),
  76. * new Date(1986, 3, 4, 11, 30, 0)
  77. * )
  78. * //=> '1 hour ago'
  79. *
  80. * @example
  81. * // Use the unit option to force the function to output the result in quarters. Without setting it, the example would return "next year"
  82. * intlFormatDistance(
  83. * new Date(1987, 6, 4, 10, 30, 0),
  84. * new Date(1986, 3, 4, 10, 30, 0),
  85. * { unit: 'quarter' }
  86. * )
  87. * //=> 'in 5 quarters'
  88. *
  89. * @example
  90. * // Use the locale option to get the result in Spanish. Without setting it, the example would return "in 1 hour".
  91. * intlFormatDistance(
  92. * new Date(1986, 3, 4, 11, 30, 0),
  93. * new Date(1986, 3, 4, 10, 30, 0),
  94. * { locale: 'es' }
  95. * )
  96. * //=> 'dentro de 1 hora'
  97. *
  98. * @example
  99. * // Use the numeric option to force the function to use numeric values. Without setting it, the example would return "tomorrow".
  100. * intlFormatDistance(
  101. * new Date(1986, 3, 5, 11, 30, 0),
  102. * new Date(1986, 3, 4, 11, 30, 0),
  103. * { numeric: 'always' }
  104. * )
  105. * //=> 'in 1 day'
  106. *
  107. * @example
  108. * // Use the style option to force the function to use short values. Without setting it, the example would return "in 2 years".
  109. * intlFormatDistance(
  110. * new Date(1988, 3, 4, 11, 30, 0),
  111. * new Date(1986, 3, 4, 11, 30, 0),
  112. * { style: 'short' }
  113. * )
  114. * //=> 'in 2 yr'
  115. */
  116. function intlFormatDistance(laterDate, earlierDate, options) {
  117. let value = 0;
  118. let unit;
  119. const [laterDate_, earlierDate_] = (0, _index.normalizeDates)(
  120. options?.in,
  121. laterDate,
  122. earlierDate,
  123. );
  124. if (!options?.unit) {
  125. // Get the unit based on diffInSeconds calculations if no unit is specified
  126. const diffInSeconds = (0, _index10.differenceInSeconds)(
  127. laterDate_,
  128. earlierDate_,
  129. ); // The smallest unit
  130. if (Math.abs(diffInSeconds) < _index2.secondsInMinute) {
  131. value = (0, _index10.differenceInSeconds)(laterDate_, earlierDate_);
  132. unit = "second";
  133. } else if (Math.abs(diffInSeconds) < _index2.secondsInHour) {
  134. value = (0, _index9.differenceInMinutes)(laterDate_, earlierDate_);
  135. unit = "minute";
  136. } else if (
  137. Math.abs(diffInSeconds) < _index2.secondsInDay &&
  138. Math.abs(
  139. (0, _index3.differenceInCalendarDays)(laterDate_, earlierDate_),
  140. ) < 1
  141. ) {
  142. value = (0, _index8.differenceInHours)(laterDate_, earlierDate_);
  143. unit = "hour";
  144. } else if (
  145. Math.abs(diffInSeconds) < _index2.secondsInWeek &&
  146. (value = (0, _index3.differenceInCalendarDays)(
  147. laterDate_,
  148. earlierDate_,
  149. )) &&
  150. Math.abs(value) < 7
  151. ) {
  152. unit = "day";
  153. } else if (Math.abs(diffInSeconds) < _index2.secondsInMonth) {
  154. value = (0, _index6.differenceInCalendarWeeks)(laterDate_, earlierDate_);
  155. unit = "week";
  156. } else if (Math.abs(diffInSeconds) < _index2.secondsInQuarter) {
  157. value = (0, _index4.differenceInCalendarMonths)(laterDate_, earlierDate_);
  158. unit = "month";
  159. } else if (Math.abs(diffInSeconds) < _index2.secondsInYear) {
  160. if (
  161. (0, _index5.differenceInCalendarQuarters)(laterDate_, earlierDate_) < 4
  162. ) {
  163. // To filter out cases that are less than a year but match 4 quarters
  164. value = (0, _index5.differenceInCalendarQuarters)(
  165. laterDate_,
  166. earlierDate_,
  167. );
  168. unit = "quarter";
  169. } else {
  170. value = (0, _index7.differenceInCalendarYears)(
  171. laterDate_,
  172. earlierDate_,
  173. );
  174. unit = "year";
  175. }
  176. } else {
  177. value = (0, _index7.differenceInCalendarYears)(laterDate_, earlierDate_);
  178. unit = "year";
  179. }
  180. } else {
  181. // Get the value if unit is specified
  182. unit = options?.unit;
  183. if (unit === "second") {
  184. value = (0, _index10.differenceInSeconds)(laterDate_, earlierDate_);
  185. } else if (unit === "minute") {
  186. value = (0, _index9.differenceInMinutes)(laterDate_, earlierDate_);
  187. } else if (unit === "hour") {
  188. value = (0, _index8.differenceInHours)(laterDate_, earlierDate_);
  189. } else if (unit === "day") {
  190. value = (0, _index3.differenceInCalendarDays)(laterDate_, earlierDate_);
  191. } else if (unit === "week") {
  192. value = (0, _index6.differenceInCalendarWeeks)(laterDate_, earlierDate_);
  193. } else if (unit === "month") {
  194. value = (0, _index4.differenceInCalendarMonths)(laterDate_, earlierDate_);
  195. } else if (unit === "quarter") {
  196. value = (0, _index5.differenceInCalendarQuarters)(
  197. laterDate_,
  198. earlierDate_,
  199. );
  200. } else if (unit === "year") {
  201. value = (0, _index7.differenceInCalendarYears)(laterDate_, earlierDate_);
  202. }
  203. }
  204. const rtf = new Intl.RelativeTimeFormat(options?.locale, {
  205. numeric: "auto",
  206. ...options,
  207. });
  208. return rtf.format(value, unit);
  209. }