index.js 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. import { getDefaultOptions } from "../_lib/defaultOptions/index.js";
  2. import getTimezoneOffsetInMilliseconds from "../_lib/getTimezoneOffsetInMilliseconds/index.js";
  3. import compareAsc from "../compareAsc/index.js";
  4. import toDate from "../toDate/index.js";
  5. import cloneObject from "../_lib/cloneObject/index.js";
  6. import assign from "../_lib/assign/index.js";
  7. import defaultLocale from "../_lib/defaultLocale/index.js";
  8. import requiredArgs from "../_lib/requiredArgs/index.js";
  9. var MILLISECONDS_IN_MINUTE = 1000 * 60;
  10. var MINUTES_IN_DAY = 60 * 24;
  11. var MINUTES_IN_MONTH = MINUTES_IN_DAY * 30;
  12. var MINUTES_IN_YEAR = MINUTES_IN_DAY * 365;
  13. /**
  14. * @name formatDistanceStrict
  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, using strict units.
  20. * This is like `formatDistance`, but does not use helpers like 'almost', 'over',
  21. * 'less than' and the like.
  22. *
  23. * | Distance between dates | Result |
  24. * |------------------------|---------------------|
  25. * | 0 ... 59 secs | [0..59] seconds |
  26. * | 1 ... 59 mins | [1..59] minutes |
  27. * | 1 ... 23 hrs | [1..23] hours |
  28. * | 1 ... 29 days | [1..29] days |
  29. * | 1 ... 11 months | [1..11] months |
  30. * | 1 ... N years | [1..N] years |
  31. *
  32. * @param {Date|Number} date - the date
  33. * @param {Date|Number} baseDate - the date to compare with
  34. * @param {Object} [options] - an object with options.
  35. * @param {Boolean} [options.addSuffix=false] - result indicates if the second date is earlier or later than the first
  36. * @param {'second'|'minute'|'hour'|'day'|'month'|'year'} [options.unit] - if specified, will force a unit
  37. * @param {'floor'|'ceil'|'round'} [options.roundingMethod='round'] - which way to round partial units
  38. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  39. * @returns {String} the distance in words
  40. * @throws {TypeError} 2 arguments required
  41. * @throws {RangeError} `date` must not be Invalid Date
  42. * @throws {RangeError} `baseDate` must not be Invalid Date
  43. * @throws {RangeError} `options.roundingMethod` must be 'floor', 'ceil' or 'round'
  44. * @throws {RangeError} `options.unit` must be 'second', 'minute', 'hour', 'day', 'month' or 'year'
  45. * @throws {RangeError} `options.locale` must contain `formatDistance` property
  46. *
  47. * @example
  48. * // What is the distance between 2 July 2014 and 1 January 2015?
  49. * const result = formatDistanceStrict(new Date(2014, 6, 2), new Date(2015, 0, 2))
  50. * //=> '6 months'
  51. *
  52. * @example
  53. * // What is the distance between 1 January 2015 00:00:15
  54. * // and 1 January 2015 00:00:00?
  55. * const result = formatDistanceStrict(
  56. * new Date(2015, 0, 1, 0, 0, 15),
  57. * new Date(2015, 0, 1, 0, 0, 0)
  58. * )
  59. * //=> '15 seconds'
  60. *
  61. * @example
  62. * // What is the distance from 1 January 2016
  63. * // to 1 January 2015, with a suffix?
  64. * const result = formatDistanceStrict(new Date(2015, 0, 1), new Date(2016, 0, 1), {
  65. * addSuffix: true
  66. * })
  67. * //=> '1 year ago'
  68. *
  69. * @example
  70. * // What is the distance from 1 January 2016
  71. * // to 1 January 2015, in minutes?
  72. * const result = formatDistanceStrict(new Date(2016, 0, 1), new Date(2015, 0, 1), {
  73. * unit: 'minute'
  74. * })
  75. * //=> '525600 minutes'
  76. *
  77. * @example
  78. * // What is the distance from 1 January 2015
  79. * // to 28 January 2015, in months, rounded up?
  80. * const result = formatDistanceStrict(new Date(2015, 0, 28), new Date(2015, 0, 1), {
  81. * unit: 'month',
  82. * roundingMethod: 'ceil'
  83. * })
  84. * //=> '1 month'
  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 = formatDistanceStrict(new Date(2016, 7, 1), new Date(2015, 0, 1), {
  90. * locale: eoLocale
  91. * })
  92. * //=> '1 jaro'
  93. */
  94. export default function formatDistanceStrict(dirtyDate, dirtyBaseDate, options) {
  95. var _ref, _options$locale, _options$roundingMeth;
  96. requiredArgs(2, arguments);
  97. var defaultOptions = getDefaultOptions();
  98. 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;
  99. if (!locale.formatDistance) {
  100. throw new RangeError('locale must contain localize.formatDistance property');
  101. }
  102. var comparison = compareAsc(dirtyDate, dirtyBaseDate);
  103. if (isNaN(comparison)) {
  104. throw new RangeError('Invalid time value');
  105. }
  106. var localizeOptions = assign(cloneObject(options), {
  107. addSuffix: Boolean(options === null || options === void 0 ? void 0 : options.addSuffix),
  108. comparison: comparison
  109. });
  110. var dateLeft;
  111. var dateRight;
  112. if (comparison > 0) {
  113. dateLeft = toDate(dirtyBaseDate);
  114. dateRight = toDate(dirtyDate);
  115. } else {
  116. dateLeft = toDate(dirtyDate);
  117. dateRight = toDate(dirtyBaseDate);
  118. }
  119. var roundingMethod = String((_options$roundingMeth = options === null || options === void 0 ? void 0 : options.roundingMethod) !== null && _options$roundingMeth !== void 0 ? _options$roundingMeth : 'round');
  120. var roundingMethodFn;
  121. if (roundingMethod === 'floor') {
  122. roundingMethodFn = Math.floor;
  123. } else if (roundingMethod === 'ceil') {
  124. roundingMethodFn = Math.ceil;
  125. } else if (roundingMethod === 'round') {
  126. roundingMethodFn = Math.round;
  127. } else {
  128. throw new RangeError("roundingMethod must be 'floor', 'ceil' or 'round'");
  129. }
  130. var milliseconds = dateRight.getTime() - dateLeft.getTime();
  131. var minutes = milliseconds / MILLISECONDS_IN_MINUTE;
  132. var timezoneOffset = getTimezoneOffsetInMilliseconds(dateRight) - getTimezoneOffsetInMilliseconds(dateLeft);
  133. // Use DST-normalized difference in minutes for years, months and days;
  134. // use regular difference in minutes for hours, minutes and seconds.
  135. var dstNormalizedMinutes = (milliseconds - timezoneOffset) / MILLISECONDS_IN_MINUTE;
  136. var defaultUnit = options === null || options === void 0 ? void 0 : options.unit;
  137. var unit;
  138. if (!defaultUnit) {
  139. if (minutes < 1) {
  140. unit = 'second';
  141. } else if (minutes < 60) {
  142. unit = 'minute';
  143. } else if (minutes < MINUTES_IN_DAY) {
  144. unit = 'hour';
  145. } else if (dstNormalizedMinutes < MINUTES_IN_MONTH) {
  146. unit = 'day';
  147. } else if (dstNormalizedMinutes < MINUTES_IN_YEAR) {
  148. unit = 'month';
  149. } else {
  150. unit = 'year';
  151. }
  152. } else {
  153. unit = String(defaultUnit);
  154. }
  155. // 0 up to 60 seconds
  156. if (unit === 'second') {
  157. var seconds = roundingMethodFn(milliseconds / 1000);
  158. return locale.formatDistance('xSeconds', seconds, localizeOptions);
  159. // 1 up to 60 mins
  160. } else if (unit === 'minute') {
  161. var roundedMinutes = roundingMethodFn(minutes);
  162. return locale.formatDistance('xMinutes', roundedMinutes, localizeOptions);
  163. // 1 up to 24 hours
  164. } else if (unit === 'hour') {
  165. var hours = roundingMethodFn(minutes / 60);
  166. return locale.formatDistance('xHours', hours, localizeOptions);
  167. // 1 up to 30 days
  168. } else if (unit === 'day') {
  169. var days = roundingMethodFn(dstNormalizedMinutes / MINUTES_IN_DAY);
  170. return locale.formatDistance('xDays', days, localizeOptions);
  171. // 1 up to 12 months
  172. } else if (unit === 'month') {
  173. var months = roundingMethodFn(dstNormalizedMinutes / MINUTES_IN_MONTH);
  174. return months === 12 && defaultUnit !== 'month' ? locale.formatDistance('xYears', 1, localizeOptions) : locale.formatDistance('xMonths', months, localizeOptions);
  175. // 1 year up to max Date
  176. } else if (unit === 'year') {
  177. var years = roundingMethodFn(dstNormalizedMinutes / MINUTES_IN_YEAR);
  178. return locale.formatDistance('xYears', years, localizeOptions);
  179. }
  180. throw new RangeError("unit must be 'second', 'minute', 'hour', 'day', 'month' or 'year'");
  181. }