index.js 4.9 KB

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