index.js 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  1. "use strict";
  2. Object.defineProperty(exports, "__esModule", {
  3. value: true
  4. });
  5. exports.default = formatDistanceToNow;
  6. var _index = _interopRequireDefault(require("../formatDistance/index.js"));
  7. var _index2 = _interopRequireDefault(require("../_lib/requiredArgs/index.js"));
  8. function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
  9. /**
  10. * @name formatDistanceToNow
  11. * @category Common Helpers
  12. * @summary Return the distance between the given date and now in words.
  13. * @pure false
  14. *
  15. * @description
  16. * Return the distance between the given date and now in words.
  17. *
  18. * | Distance to now | Result |
  19. * |-------------------------------------------------------------------|---------------------|
  20. * | 0 ... 30 secs | less than a minute |
  21. * | 30 secs ... 1 min 30 secs | 1 minute |
  22. * | 1 min 30 secs ... 44 mins 30 secs | [2..44] minutes |
  23. * | 44 mins ... 30 secs ... 89 mins 30 secs | about 1 hour |
  24. * | 89 mins 30 secs ... 23 hrs 59 mins 30 secs | about [2..24] hours |
  25. * | 23 hrs 59 mins 30 secs ... 41 hrs 59 mins 30 secs | 1 day |
  26. * | 41 hrs 59 mins 30 secs ... 29 days 23 hrs 59 mins 30 secs | [2..30] days |
  27. * | 29 days 23 hrs 59 mins 30 secs ... 44 days 23 hrs 59 mins 30 secs | about 1 month |
  28. * | 44 days 23 hrs 59 mins 30 secs ... 59 days 23 hrs 59 mins 30 secs | about 2 months |
  29. * | 59 days 23 hrs 59 mins 30 secs ... 1 yr | [2..12] months |
  30. * | 1 yr ... 1 yr 3 months | about 1 year |
  31. * | 1 yr 3 months ... 1 yr 9 month s | over 1 year |
  32. * | 1 yr 9 months ... 2 yrs | almost 2 years |
  33. * | N yrs ... N yrs 3 months | about N years |
  34. * | N yrs 3 months ... N yrs 9 months | over N years |
  35. * | N yrs 9 months ... N+1 yrs | almost N+1 years |
  36. *
  37. * With `options.includeSeconds == true`:
  38. * | Distance to now | Result |
  39. * |---------------------|----------------------|
  40. * | 0 secs ... 5 secs | less than 5 seconds |
  41. * | 5 secs ... 10 secs | less than 10 seconds |
  42. * | 10 secs ... 20 secs | less than 20 seconds |
  43. * | 20 secs ... 40 secs | half a minute |
  44. * | 40 secs ... 60 secs | less than a minute |
  45. * | 60 secs ... 90 secs | 1 minute |
  46. *
  47. * > ⚠️ Please note that this function is not present in the FP submodule as
  48. * > it uses `Date.now()` internally hence impure and can't be safely curried.
  49. *
  50. * ### v2.0.0 breaking changes:
  51. *
  52. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  53. *
  54. * - The function was renamed from `distanceInWordsToNow ` to `formatDistanceToNow`
  55. * to make its name consistent with `format` and `formatRelative`.
  56. *
  57. * ```javascript
  58. * // Before v2.0.0
  59. *
  60. * distanceInWordsToNow(new Date(2014, 6, 2), { addSuffix: true })
  61. * //=> 'in 6 months'
  62. *
  63. * // v2.0.0 onward
  64. *
  65. * formatDistanceToNow(new Date(2014, 6, 2), { addSuffix: true })
  66. * //=> 'in 6 months'
  67. * ```
  68. *
  69. * @param {Date|Number} date - the given date
  70. * @param {Object} [options] - the object with options
  71. * @param {Boolean} [options.includeSeconds=false] - distances less than a minute are more detailed
  72. * @param {Boolean} [options.addSuffix=false] - result specifies if now is earlier or later than the passed date
  73. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  74. * @returns {String} the distance in words
  75. * @throws {TypeError} 1 argument required
  76. * @throws {RangeError} `date` must not be Invalid Date
  77. * @throws {RangeError} `options.locale` must contain `formatDistance` property
  78. *
  79. * @example
  80. * // If today is 1 January 2015, what is the distance to 2 July 2014?
  81. * var result = formatDistanceToNow(
  82. * new Date(2014, 6, 2)
  83. * )
  84. * //=> '6 months'
  85. *
  86. * @example
  87. * // If now is 1 January 2015 00:00:00,
  88. * // what is the distance to 1 January 2015 00:00:15, including seconds?
  89. * var result = formatDistanceToNow(
  90. * new Date(2015, 0, 1, 0, 0, 15),
  91. * {includeSeconds: true}
  92. * )
  93. * //=> 'less than 20 seconds'
  94. *
  95. * @example
  96. * // If today is 1 January 2015,
  97. * // what is the distance to 1 January 2016, with a suffix?
  98. * var result = formatDistanceToNow(
  99. * new Date(2016, 0, 1),
  100. * {addSuffix: true}
  101. * )
  102. * //=> 'in about 1 year'
  103. *
  104. * @example
  105. * // If today is 1 January 2015,
  106. * // what is the distance to 1 August 2016 in Esperanto?
  107. * var eoLocale = require('date-fns/locale/eo')
  108. * var result = formatDistanceToNow(
  109. * new Date(2016, 7, 1),
  110. * {locale: eoLocale}
  111. * )
  112. * //=> 'pli ol 1 jaro'
  113. */
  114. function formatDistanceToNow(dirtyDate, dirtyOptions) {
  115. (0, _index2.default)(1, arguments);
  116. return (0, _index.default)(dirtyDate, Date.now(), dirtyOptions);
  117. }
  118. module.exports = exports.default;