index.js 8.8 KB

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