formatDistanceStrict.cjs 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  1. "use strict";
  2. exports.formatDistanceStrict = formatDistanceStrict;
  3. var _index = require("./_lib/defaultLocale.cjs");
  4. var _index2 = require("./_lib/defaultOptions.cjs");
  5. var _index3 = require("./_lib/getRoundingMethod.cjs");
  6. var _index4 = require("./_lib/getTimezoneOffsetInMilliseconds.cjs");
  7. var _index5 = require("./_lib/normalizeDates.cjs");
  8. var _index6 = require("./compareAsc.cjs");
  9. var _index7 = require("./constants.cjs");
  10. /**
  11. * The {@link formatDistanceStrict} function options.
  12. */
  13. /**
  14. * The unit used to format the distance in {@link formatDistanceStrict}.
  15. */
  16. /**
  17. * @name formatDistanceStrict
  18. * @category Common Helpers
  19. * @summary Return the distance between the given dates in words.
  20. *
  21. * @description
  22. * Return the distance between the given dates in words, using strict units.
  23. * This is like `formatDistance`, but does not use helpers like 'almost', 'over',
  24. * 'less than' and the like.
  25. *
  26. * | Distance between dates | Result |
  27. * |------------------------|---------------------|
  28. * | 0 ... 59 secs | [0..59] seconds |
  29. * | 1 ... 59 mins | [1..59] minutes |
  30. * | 1 ... 23 hrs | [1..23] hours |
  31. * | 1 ... 29 days | [1..29] days |
  32. * | 1 ... 11 months | [1..11] months |
  33. * | 1 ... N years | [1..N] years |
  34. *
  35. * @param laterDate - The date
  36. * @param earlierDate - The date to compare with
  37. * @param options - An object with options
  38. *
  39. * @returns The distance in words
  40. *
  41. * @throws `date` must not be Invalid Date
  42. * @throws `baseDate` must not be Invalid Date
  43. * @throws `options.unit` must be 'second', 'minute', 'hour', 'day', 'month' or 'year'
  44. * @throws `options.locale` must contain `formatDistance` property
  45. *
  46. * @example
  47. * // What is the distance between 2 July 2014 and 1 January 2015?
  48. * const result = formatDistanceStrict(new Date(2014, 6, 2), new Date(2015, 0, 2))
  49. * //=> '6 months'
  50. *
  51. * @example
  52. * // What is the distance between 1 January 2015 00:00:15
  53. * // and 1 January 2015 00:00:00?
  54. * const result = formatDistanceStrict(
  55. * new Date(2015, 0, 1, 0, 0, 15),
  56. * new Date(2015, 0, 1, 0, 0, 0)
  57. * )
  58. * //=> '15 seconds'
  59. *
  60. * @example
  61. * // What is the distance from 1 January 2016
  62. * // to 1 January 2015, with a suffix?
  63. * const result = formatDistanceStrict(new Date(2015, 0, 1), new Date(2016, 0, 1), {
  64. * addSuffix: true
  65. * })
  66. * //=> '1 year ago'
  67. *
  68. * @example
  69. * // What is the distance from 1 January 2016
  70. * // to 1 January 2015, in minutes?
  71. * const result = formatDistanceStrict(new Date(2016, 0, 1), new Date(2015, 0, 1), {
  72. * unit: 'minute'
  73. * })
  74. * //=> '525600 minutes'
  75. *
  76. * @example
  77. * // What is the distance from 1 January 2015
  78. * // to 28 January 2015, in months, rounded up?
  79. * const result = formatDistanceStrict(new Date(2015, 0, 28), new Date(2015, 0, 1), {
  80. * unit: 'month',
  81. * roundingMethod: 'ceil'
  82. * })
  83. * //=> '1 month'
  84. *
  85. * @example
  86. * // What is the distance between 1 August 2016 and 1 January 2015 in Esperanto?
  87. * import { eoLocale } from 'date-fns/locale/eo'
  88. * const result = formatDistanceStrict(new Date(2016, 7, 1), new Date(2015, 0, 1), {
  89. * locale: eoLocale
  90. * })
  91. * //=> '1 jaro'
  92. */
  93. function formatDistanceStrict(laterDate, earlierDate, options) {
  94. const defaultOptions = (0, _index2.getDefaultOptions)();
  95. const locale =
  96. options?.locale ?? defaultOptions.locale ?? _index.defaultLocale;
  97. const comparison = (0, _index6.compareAsc)(laterDate, earlierDate);
  98. if (isNaN(comparison)) {
  99. throw new RangeError("Invalid time value");
  100. }
  101. const localizeOptions = Object.assign({}, options, {
  102. addSuffix: options?.addSuffix,
  103. comparison: comparison,
  104. });
  105. const [laterDate_, earlierDate_] = (0, _index5.normalizeDates)(
  106. options?.in,
  107. ...(comparison > 0 ? [earlierDate, laterDate] : [laterDate, earlierDate]),
  108. );
  109. const roundingMethod = (0, _index3.getRoundingMethod)(
  110. options?.roundingMethod ?? "round",
  111. );
  112. const milliseconds = earlierDate_.getTime() - laterDate_.getTime();
  113. const minutes = milliseconds / _index7.millisecondsInMinute;
  114. const timezoneOffset =
  115. (0, _index4.getTimezoneOffsetInMilliseconds)(earlierDate_) -
  116. (0, _index4.getTimezoneOffsetInMilliseconds)(laterDate_);
  117. // Use DST-normalized difference in minutes for years, months and days;
  118. // use regular difference in minutes for hours, minutes and seconds.
  119. const dstNormalizedMinutes =
  120. (milliseconds - timezoneOffset) / _index7.millisecondsInMinute;
  121. const defaultUnit = options?.unit;
  122. let unit;
  123. if (!defaultUnit) {
  124. if (minutes < 1) {
  125. unit = "second";
  126. } else if (minutes < 60) {
  127. unit = "minute";
  128. } else if (minutes < _index7.minutesInDay) {
  129. unit = "hour";
  130. } else if (dstNormalizedMinutes < _index7.minutesInMonth) {
  131. unit = "day";
  132. } else if (dstNormalizedMinutes < _index7.minutesInYear) {
  133. unit = "month";
  134. } else {
  135. unit = "year";
  136. }
  137. } else {
  138. unit = defaultUnit;
  139. }
  140. // 0 up to 60 seconds
  141. if (unit === "second") {
  142. const seconds = roundingMethod(milliseconds / 1000);
  143. return locale.formatDistance("xSeconds", seconds, localizeOptions);
  144. // 1 up to 60 mins
  145. } else if (unit === "minute") {
  146. const roundedMinutes = roundingMethod(minutes);
  147. return locale.formatDistance("xMinutes", roundedMinutes, localizeOptions);
  148. // 1 up to 24 hours
  149. } else if (unit === "hour") {
  150. const hours = roundingMethod(minutes / 60);
  151. return locale.formatDistance("xHours", hours, localizeOptions);
  152. // 1 up to 30 days
  153. } else if (unit === "day") {
  154. const days = roundingMethod(dstNormalizedMinutes / _index7.minutesInDay);
  155. return locale.formatDistance("xDays", days, localizeOptions);
  156. // 1 up to 12 months
  157. } else if (unit === "month") {
  158. const months = roundingMethod(
  159. dstNormalizedMinutes / _index7.minutesInMonth,
  160. );
  161. return months === 12 && defaultUnit !== "month"
  162. ? locale.formatDistance("xYears", 1, localizeOptions)
  163. : locale.formatDistance("xMonths", months, localizeOptions);
  164. // 1 year up to max Date
  165. } else {
  166. const years = roundingMethod(dstNormalizedMinutes / _index7.minutesInYear);
  167. return locale.formatDistance("xYears", years, localizeOptions);
  168. }
  169. }