index.js 9.3 KB

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