index.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. import compareAsc from "../compareAsc/index.js";
  2. import differenceInMonths from "../differenceInMonths/index.js";
  3. import differenceInSeconds from "../differenceInSeconds/index.js";
  4. import defaultLocale from "../locale/en-US/index.js";
  5. import toDate from "../toDate/index.js";
  6. import cloneObject from "../_lib/cloneObject/index.js";
  7. import getTimezoneOffsetInMilliseconds from "../_lib/getTimezoneOffsetInMilliseconds/index.js";
  8. import requiredArgs from "../_lib/requiredArgs/index.js";
  9. var MINUTES_IN_DAY = 1440;
  10. var MINUTES_IN_ALMOST_TWO_DAYS = 2520;
  11. var MINUTES_IN_MONTH = 43200;
  12. var MINUTES_IN_TWO_MONTHS = 86400;
  13. /**
  14. * @name formatDistance
  15. * @category Common Helpers
  16. * @summary Return the distance between the given dates in words.
  17. *
  18. * @description
  19. * Return the distance between the given dates in words.
  20. *
  21. * | Distance between dates | Result |
  22. * |-------------------------------------------------------------------|---------------------|
  23. * | 0 ... 30 secs | less than a minute |
  24. * | 30 secs ... 1 min 30 secs | 1 minute |
  25. * | 1 min 30 secs ... 44 mins 30 secs | [2..44] minutes |
  26. * | 44 mins ... 30 secs ... 89 mins 30 secs | about 1 hour |
  27. * | 89 mins 30 secs ... 23 hrs 59 mins 30 secs | about [2..24] hours |
  28. * | 23 hrs 59 mins 30 secs ... 41 hrs 59 mins 30 secs | 1 day |
  29. * | 41 hrs 59 mins 30 secs ... 29 days 23 hrs 59 mins 30 secs | [2..30] days |
  30. * | 29 days 23 hrs 59 mins 30 secs ... 44 days 23 hrs 59 mins 30 secs | about 1 month |
  31. * | 44 days 23 hrs 59 mins 30 secs ... 59 days 23 hrs 59 mins 30 secs | about 2 months |
  32. * | 59 days 23 hrs 59 mins 30 secs ... 1 yr | [2..12] months |
  33. * | 1 yr ... 1 yr 3 months | about 1 year |
  34. * | 1 yr 3 months ... 1 yr 9 month s | over 1 year |
  35. * | 1 yr 9 months ... 2 yrs | almost 2 years |
  36. * | N yrs ... N yrs 3 months | about N years |
  37. * | N yrs 3 months ... N yrs 9 months | over N years |
  38. * | N yrs 9 months ... N+1 yrs | almost N+1 years |
  39. *
  40. * With `options.includeSeconds == true`:
  41. * | Distance between dates | Result |
  42. * |------------------------|----------------------|
  43. * | 0 secs ... 5 secs | less than 5 seconds |
  44. * | 5 secs ... 10 secs | less than 10 seconds |
  45. * | 10 secs ... 20 secs | less than 20 seconds |
  46. * | 20 secs ... 40 secs | half a minute |
  47. * | 40 secs ... 60 secs | less than a minute |
  48. * | 60 secs ... 90 secs | 1 minute |
  49. *
  50. * ### v2.0.0 breaking changes:
  51. *
  52. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  53. *
  54. * - The function was renamed from `distanceInWords ` to `formatDistance`
  55. * to make its name consistent with `format` and `formatRelative`.
  56. *
  57. * - The order of arguments is swapped to make the function
  58. * consistent with `differenceIn...` functions.
  59. *
  60. * ```javascript
  61. * // Before v2.0.0
  62. *
  63. * distanceInWords(
  64. * new Date(1986, 3, 4, 10, 32, 0),
  65. * new Date(1986, 3, 4, 11, 32, 0),
  66. * { addSuffix: true }
  67. * ) //=> 'in about 1 hour'
  68. *
  69. * // v2.0.0 onward
  70. *
  71. * formatDistance(
  72. * new Date(1986, 3, 4, 11, 32, 0),
  73. * new Date(1986, 3, 4, 10, 32, 0),
  74. * { addSuffix: true }
  75. * ) //=> 'in about 1 hour'
  76. * ```
  77. *
  78. * @param {Date|Number} date - the date
  79. * @param {Date|Number} baseDate - the date to compare with
  80. * @param {Object} [options] - an object with options.
  81. * @param {Boolean} [options.includeSeconds=false] - distances less than a minute are more detailed
  82. * @param {Boolean} [options.addSuffix=false] - result indicates if the second date is earlier or later than the first
  83. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  84. * @returns {String} the distance in words
  85. * @throws {TypeError} 2 arguments required
  86. * @throws {RangeError} `date` must not be Invalid Date
  87. * @throws {RangeError} `baseDate` must not be Invalid Date
  88. * @throws {RangeError} `options.locale` must contain `formatDistance` property
  89. *
  90. * @example
  91. * // What is the distance between 2 July 2014 and 1 January 2015?
  92. * const result = formatDistance(new Date(2014, 6, 2), new Date(2015, 0, 1))
  93. * //=> '6 months'
  94. *
  95. * @example
  96. * // What is the distance between 1 January 2015 00:00:15
  97. * // and 1 January 2015 00:00:00, including seconds?
  98. * const result = formatDistance(
  99. * new Date(2015, 0, 1, 0, 0, 15),
  100. * new Date(2015, 0, 1, 0, 0, 0),
  101. * { includeSeconds: true }
  102. * )
  103. * //=> 'less than 20 seconds'
  104. *
  105. * @example
  106. * // What is the distance from 1 January 2016
  107. * // to 1 January 2015, with a suffix?
  108. * const result = formatDistance(new Date(2015, 0, 1), new Date(2016, 0, 1), {
  109. * addSuffix: true
  110. * })
  111. * //=> 'about 1 year ago'
  112. *
  113. * @example
  114. * // What is the distance between 1 August 2016 and 1 January 2015 in Esperanto?
  115. * import { eoLocale } from 'date-fns/locale/eo'
  116. * const result = formatDistance(new Date(2016, 7, 1), new Date(2015, 0, 1), {
  117. * locale: eoLocale
  118. * })
  119. * //=> 'pli ol 1 jaro'
  120. */
  121. export default function formatDistance(dirtyDate, dirtyBaseDate) {
  122. var options = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : {};
  123. requiredArgs(2, arguments);
  124. var locale = options.locale || defaultLocale;
  125. if (!locale.formatDistance) {
  126. throw new RangeError('locale must contain formatDistance property');
  127. }
  128. var comparison = compareAsc(dirtyDate, dirtyBaseDate);
  129. if (isNaN(comparison)) {
  130. throw new RangeError('Invalid time value');
  131. }
  132. var localizeOptions = cloneObject(options);
  133. localizeOptions.addSuffix = Boolean(options.addSuffix);
  134. localizeOptions.comparison = comparison;
  135. var dateLeft;
  136. var dateRight;
  137. if (comparison > 0) {
  138. dateLeft = toDate(dirtyBaseDate);
  139. dateRight = toDate(dirtyDate);
  140. } else {
  141. dateLeft = toDate(dirtyDate);
  142. dateRight = toDate(dirtyBaseDate);
  143. }
  144. var seconds = differenceInSeconds(dateRight, dateLeft);
  145. var offsetInSeconds = (getTimezoneOffsetInMilliseconds(dateRight) - getTimezoneOffsetInMilliseconds(dateLeft)) / 1000;
  146. var minutes = Math.round((seconds - offsetInSeconds) / 60);
  147. var months; // 0 up to 2 mins
  148. if (minutes < 2) {
  149. if (options.includeSeconds) {
  150. if (seconds < 5) {
  151. return locale.formatDistance('lessThanXSeconds', 5, localizeOptions);
  152. } else if (seconds < 10) {
  153. return locale.formatDistance('lessThanXSeconds', 10, localizeOptions);
  154. } else if (seconds < 20) {
  155. return locale.formatDistance('lessThanXSeconds', 20, localizeOptions);
  156. } else if (seconds < 40) {
  157. return locale.formatDistance('halfAMinute', null, localizeOptions);
  158. } else if (seconds < 60) {
  159. return locale.formatDistance('lessThanXMinutes', 1, localizeOptions);
  160. } else {
  161. return locale.formatDistance('xMinutes', 1, localizeOptions);
  162. }
  163. } else {
  164. if (minutes === 0) {
  165. return locale.formatDistance('lessThanXMinutes', 1, localizeOptions);
  166. } else {
  167. return locale.formatDistance('xMinutes', minutes, localizeOptions);
  168. }
  169. } // 2 mins up to 0.75 hrs
  170. } else if (minutes < 45) {
  171. return locale.formatDistance('xMinutes', minutes, localizeOptions); // 0.75 hrs up to 1.5 hrs
  172. } else if (minutes < 90) {
  173. return locale.formatDistance('aboutXHours', 1, localizeOptions); // 1.5 hrs up to 24 hrs
  174. } else if (minutes < MINUTES_IN_DAY) {
  175. var hours = Math.round(minutes / 60);
  176. return locale.formatDistance('aboutXHours', hours, localizeOptions); // 1 day up to 1.75 days
  177. } else if (minutes < MINUTES_IN_ALMOST_TWO_DAYS) {
  178. return locale.formatDistance('xDays', 1, localizeOptions); // 1.75 days up to 30 days
  179. } else if (minutes < MINUTES_IN_MONTH) {
  180. var days = Math.round(minutes / MINUTES_IN_DAY);
  181. return locale.formatDistance('xDays', days, localizeOptions); // 1 month up to 2 months
  182. } else if (minutes < MINUTES_IN_TWO_MONTHS) {
  183. months = Math.round(minutes / MINUTES_IN_MONTH);
  184. return locale.formatDistance('aboutXMonths', months, localizeOptions);
  185. }
  186. months = differenceInMonths(dateRight, dateLeft); // 2 months up to 12 months
  187. if (months < 12) {
  188. var nearestMonth = Math.round(minutes / MINUTES_IN_MONTH);
  189. return locale.formatDistance('xMonths', nearestMonth, localizeOptions); // 1 year up to max Date
  190. } else {
  191. var monthsSinceStartOfYear = months % 12;
  192. var years = Math.floor(months / 12); // N years up to 1 years 3 months
  193. if (monthsSinceStartOfYear < 3) {
  194. return locale.formatDistance('aboutXYears', years, localizeOptions); // N years 3 months up to N years 9 months
  195. } else if (monthsSinceStartOfYear < 9) {
  196. return locale.formatDistance('overXYears', years, localizeOptions); // N years 9 months up to N year 12 months
  197. } else {
  198. return locale.formatDistance('almostXYears', years + 1, localizeOptions);
  199. }
  200. }
  201. }