formatDistanceToNow.js 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. "use strict";
  2. exports.formatDistanceToNow = formatDistanceToNow;
  3. var _index = require("./constructNow.js");
  4. var _index2 = require("./formatDistance.js");
  5. /**
  6. * The {@link formatDistanceToNow} function options.
  7. */
  8. /**
  9. * @name formatDistanceToNow
  10. * @category Common Helpers
  11. * @summary Return the distance between the given date and now in words.
  12. * @pure false
  13. *
  14. * @description
  15. * Return the distance between the given date and now in words.
  16. *
  17. * | Distance to now | Result |
  18. * |-------------------------------------------------------------------|---------------------|
  19. * | 0 ... 30 secs | less than a minute |
  20. * | 30 secs ... 1 min 30 secs | 1 minute |
  21. * | 1 min 30 secs ... 44 mins 30 secs | [2..44] minutes |
  22. * | 44 mins ... 30 secs ... 89 mins 30 secs | about 1 hour |
  23. * | 89 mins 30 secs ... 23 hrs 59 mins 30 secs | about [2..24] hours |
  24. * | 23 hrs 59 mins 30 secs ... 41 hrs 59 mins 30 secs | 1 day |
  25. * | 41 hrs 59 mins 30 secs ... 29 days 23 hrs 59 mins 30 secs | [2..30] days |
  26. * | 29 days 23 hrs 59 mins 30 secs ... 44 days 23 hrs 59 mins 30 secs | about 1 month |
  27. * | 44 days 23 hrs 59 mins 30 secs ... 59 days 23 hrs 59 mins 30 secs | about 2 months |
  28. * | 59 days 23 hrs 59 mins 30 secs ... 1 yr | [2..12] months |
  29. * | 1 yr ... 1 yr 3 months | about 1 year |
  30. * | 1 yr 3 months ... 1 yr 9 month s | over 1 year |
  31. * | 1 yr 9 months ... 2 yrs | almost 2 years |
  32. * | N yrs ... N yrs 3 months | about N years |
  33. * | N yrs 3 months ... N yrs 9 months | over N years |
  34. * | N yrs 9 months ... N+1 yrs | almost N+1 years |
  35. *
  36. * With `options.includeSeconds == true`:
  37. * | Distance to now | Result |
  38. * |---------------------|----------------------|
  39. * | 0 secs ... 5 secs | less than 5 seconds |
  40. * | 5 secs ... 10 secs | less than 10 seconds |
  41. * | 10 secs ... 20 secs | less than 20 seconds |
  42. * | 20 secs ... 40 secs | half a minute |
  43. * | 40 secs ... 60 secs | less than a minute |
  44. * | 60 secs ... 90 secs | 1 minute |
  45. *
  46. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  47. *
  48. * @param date - The given date
  49. * @param options - The object with options
  50. *
  51. * @returns The distance in words
  52. *
  53. * @throws `date` must not be Invalid Date
  54. * @throws `options.locale` must contain `formatDistance` property
  55. *
  56. * @example
  57. * // If today is 1 January 2015, what is the distance to 2 July 2014?
  58. * const result = formatDistanceToNow(
  59. * new Date(2014, 6, 2)
  60. * )
  61. * //=> '6 months'
  62. *
  63. * @example
  64. * // If now is 1 January 2015 00:00:00,
  65. * // what is the distance to 1 January 2015 00:00:15, including seconds?
  66. * const result = formatDistanceToNow(
  67. * new Date(2015, 0, 1, 0, 0, 15),
  68. * {includeSeconds: true}
  69. * )
  70. * //=> 'less than 20 seconds'
  71. *
  72. * @example
  73. * // If today is 1 January 2015,
  74. * // what is the distance to 1 January 2016, with a suffix?
  75. * const result = formatDistanceToNow(
  76. * new Date(2016, 0, 1),
  77. * {addSuffix: true}
  78. * )
  79. * //=> 'in about 1 year'
  80. *
  81. * @example
  82. * // If today is 1 January 2015,
  83. * // what is the distance to 1 August 2016 in Esperanto?
  84. * const eoLocale = require('date-fns/locale/eo')
  85. * const result = formatDistanceToNow(
  86. * new Date(2016, 7, 1),
  87. * {locale: eoLocale}
  88. * )
  89. * //=> 'pli ol 1 jaro'
  90. */
  91. function formatDistanceToNow(date, options) {
  92. return (0, _index2.formatDistance)(
  93. date,
  94. (0, _index.constructNow)(date),
  95. options,
  96. );
  97. }