formatDistanceToNow.js 3.8 KB

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