formatDistance.js 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. "use strict";
  2. exports.formatDistance = formatDistance;
  3. var _index = require("./compareAsc.js");
  4. var _index2 = require("./constants.js");
  5. var _index3 = require("./differenceInMonths.js");
  6. var _index4 = require("./differenceInSeconds.js");
  7. var _index5 = require("./toDate.js");
  8. var _index6 = require("./_lib/defaultLocale.js");
  9. var _index7 = require("./_lib/defaultOptions.js");
  10. var _index8 = require("./_lib/getTimezoneOffsetInMilliseconds.js");
  11. /**
  12. * The {@link formatDistance} function options.
  13. */
  14. /**
  15. * @name formatDistance
  16. * @category Common Helpers
  17. * @summary Return the distance between the given dates in words.
  18. *
  19. * @description
  20. * Return the distance between the given dates in words.
  21. *
  22. * | Distance between dates | Result |
  23. * |-------------------------------------------------------------------|---------------------|
  24. * | 0 ... 30 secs | less than a minute |
  25. * | 30 secs ... 1 min 30 secs | 1 minute |
  26. * | 1 min 30 secs ... 44 mins 30 secs | [2..44] minutes |
  27. * | 44 mins ... 30 secs ... 89 mins 30 secs | about 1 hour |
  28. * | 89 mins 30 secs ... 23 hrs 59 mins 30 secs | about [2..24] hours |
  29. * | 23 hrs 59 mins 30 secs ... 41 hrs 59 mins 30 secs | 1 day |
  30. * | 41 hrs 59 mins 30 secs ... 29 days 23 hrs 59 mins 30 secs | [2..30] days |
  31. * | 29 days 23 hrs 59 mins 30 secs ... 44 days 23 hrs 59 mins 30 secs | about 1 month |
  32. * | 44 days 23 hrs 59 mins 30 secs ... 59 days 23 hrs 59 mins 30 secs | about 2 months |
  33. * | 59 days 23 hrs 59 mins 30 secs ... 1 yr | [2..12] months |
  34. * | 1 yr ... 1 yr 3 months | about 1 year |
  35. * | 1 yr 3 months ... 1 yr 9 month s | over 1 year |
  36. * | 1 yr 9 months ... 2 yrs | almost 2 years |
  37. * | N yrs ... N yrs 3 months | about N years |
  38. * | N yrs 3 months ... N yrs 9 months | over N years |
  39. * | N yrs 9 months ... N+1 yrs | almost N+1 years |
  40. *
  41. * With `options.includeSeconds == true`:
  42. * | Distance between dates | Result |
  43. * |------------------------|----------------------|
  44. * | 0 secs ... 5 secs | less than 5 seconds |
  45. * | 5 secs ... 10 secs | less than 10 seconds |
  46. * | 10 secs ... 20 secs | less than 20 seconds |
  47. * | 20 secs ... 40 secs | half a minute |
  48. * | 40 secs ... 60 secs | less than a minute |
  49. * | 60 secs ... 90 secs | 1 minute |
  50. *
  51. * @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).
  52. *
  53. * @param date - The date
  54. * @param baseDate - The date to compare with
  55. * @param options - An object with options
  56. *
  57. * @returns The distance in words
  58. *
  59. * @throws `date` must not be Invalid Date
  60. * @throws `baseDate` must not be Invalid Date
  61. * @throws `options.locale` must contain `formatDistance` property
  62. *
  63. * @example
  64. * // What is the distance between 2 July 2014 and 1 January 2015?
  65. * const result = formatDistance(new Date(2014, 6, 2), new Date(2015, 0, 1))
  66. * //=> '6 months'
  67. *
  68. * @example
  69. * // What is the distance between 1 January 2015 00:00:15
  70. * // and 1 January 2015 00:00:00, including seconds?
  71. * const result = formatDistance(
  72. * new Date(2015, 0, 1, 0, 0, 15),
  73. * new Date(2015, 0, 1, 0, 0, 0),
  74. * { includeSeconds: true }
  75. * )
  76. * //=> 'less than 20 seconds'
  77. *
  78. * @example
  79. * // What is the distance from 1 January 2016
  80. * // to 1 January 2015, with a suffix?
  81. * const result = formatDistance(new Date(2015, 0, 1), new Date(2016, 0, 1), {
  82. * addSuffix: true
  83. * })
  84. * //=> 'about 1 year ago'
  85. *
  86. * @example
  87. * // What is the distance between 1 August 2016 and 1 January 2015 in Esperanto?
  88. * import { eoLocale } from 'date-fns/locale/eo'
  89. * const result = formatDistance(new Date(2016, 7, 1), new Date(2015, 0, 1), {
  90. * locale: eoLocale
  91. * })
  92. * //=> 'pli ol 1 jaro'
  93. */
  94. function formatDistance(date, baseDate, options) {
  95. const defaultOptions = (0, _index7.getDefaultOptions)();
  96. const locale =
  97. options?.locale ?? defaultOptions.locale ?? _index6.defaultLocale;
  98. const minutesInAlmostTwoDays = 2520;
  99. const comparison = (0, _index.compareAsc)(date, baseDate);
  100. if (isNaN(comparison)) {
  101. throw new RangeError("Invalid time value");
  102. }
  103. const localizeOptions = Object.assign({}, options, {
  104. addSuffix: options?.addSuffix,
  105. comparison: comparison,
  106. });
  107. let dateLeft;
  108. let dateRight;
  109. if (comparison > 0) {
  110. dateLeft = (0, _index5.toDate)(baseDate);
  111. dateRight = (0, _index5.toDate)(date);
  112. } else {
  113. dateLeft = (0, _index5.toDate)(date);
  114. dateRight = (0, _index5.toDate)(baseDate);
  115. }
  116. const seconds = (0, _index4.differenceInSeconds)(dateRight, dateLeft);
  117. const offsetInSeconds =
  118. ((0, _index8.getTimezoneOffsetInMilliseconds)(dateRight) -
  119. (0, _index8.getTimezoneOffsetInMilliseconds)(dateLeft)) /
  120. 1000;
  121. const minutes = Math.round((seconds - offsetInSeconds) / 60);
  122. let months;
  123. // 0 up to 2 mins
  124. if (minutes < 2) {
  125. if (options?.includeSeconds) {
  126. if (seconds < 5) {
  127. return locale.formatDistance("lessThanXSeconds", 5, localizeOptions);
  128. } else if (seconds < 10) {
  129. return locale.formatDistance("lessThanXSeconds", 10, localizeOptions);
  130. } else if (seconds < 20) {
  131. return locale.formatDistance("lessThanXSeconds", 20, localizeOptions);
  132. } else if (seconds < 40) {
  133. return locale.formatDistance("halfAMinute", 0, localizeOptions);
  134. } else if (seconds < 60) {
  135. return locale.formatDistance("lessThanXMinutes", 1, localizeOptions);
  136. } else {
  137. return locale.formatDistance("xMinutes", 1, localizeOptions);
  138. }
  139. } else {
  140. if (minutes === 0) {
  141. return locale.formatDistance("lessThanXMinutes", 1, localizeOptions);
  142. } else {
  143. return locale.formatDistance("xMinutes", minutes, localizeOptions);
  144. }
  145. }
  146. // 2 mins up to 0.75 hrs
  147. } else if (minutes < 45) {
  148. return locale.formatDistance("xMinutes", minutes, localizeOptions);
  149. // 0.75 hrs up to 1.5 hrs
  150. } else if (minutes < 90) {
  151. return locale.formatDistance("aboutXHours", 1, localizeOptions);
  152. // 1.5 hrs up to 24 hrs
  153. } else if (minutes < _index2.minutesInDay) {
  154. const hours = Math.round(minutes / 60);
  155. return locale.formatDistance("aboutXHours", hours, localizeOptions);
  156. // 1 day up to 1.75 days
  157. } else if (minutes < minutesInAlmostTwoDays) {
  158. return locale.formatDistance("xDays", 1, localizeOptions);
  159. // 1.75 days up to 30 days
  160. } else if (minutes < _index2.minutesInMonth) {
  161. const days = Math.round(minutes / _index2.minutesInDay);
  162. return locale.formatDistance("xDays", days, localizeOptions);
  163. // 1 month up to 2 months
  164. } else if (minutes < _index2.minutesInMonth * 2) {
  165. months = Math.round(minutes / _index2.minutesInMonth);
  166. return locale.formatDistance("aboutXMonths", months, localizeOptions);
  167. }
  168. months = (0, _index3.differenceInMonths)(dateRight, dateLeft);
  169. // 2 months up to 12 months
  170. if (months < 12) {
  171. const nearestMonth = Math.round(minutes / _index2.minutesInMonth);
  172. return locale.formatDistance("xMonths", nearestMonth, localizeOptions);
  173. // 1 year up to max Date
  174. } else {
  175. const monthsSinceStartOfYear = months % 12;
  176. const years = Math.trunc(months / 12);
  177. // N years up to 1 years 3 months
  178. if (monthsSinceStartOfYear < 3) {
  179. return locale.formatDistance("aboutXYears", years, localizeOptions);
  180. // N years 3 months up to N years 9 months
  181. } else if (monthsSinceStartOfYear < 9) {
  182. return locale.formatDistance("overXYears", years, localizeOptions);
  183. // N years 9 months up to N year 12 months
  184. } else {
  185. return locale.formatDistance("almostXYears", years + 1, localizeOptions);
  186. }
  187. }
  188. }