index.js 9.7 KB

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