formatDistanceToNowStrict.js 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. import { constructNow } from "./constructNow.js";
  2. import { formatDistanceStrict } from "./formatDistanceStrict.js";
  3. /**
  4. * The {@link formatDistanceToNowStrict} function options.
  5. */
  6. /**
  7. * @name formatDistanceToNowStrict
  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 dates in words, using strict units.
  14. * This is like `formatDistance`, but does not use helpers like 'almost', 'over',
  15. * 'less than' and the like.
  16. *
  17. * | Distance between dates | Result |
  18. * |------------------------|---------------------|
  19. * | 0 ... 59 secs | [0..59] seconds |
  20. * | 1 ... 59 mins | [1..59] minutes |
  21. * | 1 ... 23 hrs | [1..23] hours |
  22. * | 1 ... 29 days | [1..29] days |
  23. * | 1 ... 11 months | [1..11] months |
  24. * | 1 ... N years | [1..N] years |
  25. *
  26. * @param date - The given date
  27. * @param options - An object with options.
  28. *
  29. * @returns The distance in words
  30. *
  31. * @throws `date` must not be Invalid Date
  32. * @throws `options.locale` must contain `formatDistance` property
  33. *
  34. * @example
  35. * // If today is 1 January 2015, what is the distance to 2 July 2014?
  36. * const result = formatDistanceToNowStrict(
  37. * new Date(2014, 6, 2)
  38. * )
  39. * //=> '6 months'
  40. *
  41. * @example
  42. * // If now is 1 January 2015 00:00:00,
  43. * // what is the distance to 1 January 2015 00:00:15, including seconds?
  44. * const result = formatDistanceToNowStrict(
  45. * new Date(2015, 0, 1, 0, 0, 15)
  46. * )
  47. * //=> '15 seconds'
  48. *
  49. * @example
  50. * // If today is 1 January 2015,
  51. * // what is the distance to 1 January 2016, with a suffix?
  52. * const result = formatDistanceToNowStrict(
  53. * new Date(2016, 0, 1),
  54. * {addSuffix: true}
  55. * )
  56. * //=> 'in 1 year'
  57. *
  58. * @example
  59. * // If today is 28 January 2015,
  60. * // what is the distance to 1 January 2015, in months, rounded up??
  61. * const result = formatDistanceToNowStrict(new Date(2015, 0, 1), {
  62. * unit: 'month',
  63. * roundingMethod: 'ceil'
  64. * })
  65. * //=> '1 month'
  66. *
  67. * @example
  68. * // If today is 1 January 2015,
  69. * // what is the distance to 1 January 2016 in Esperanto?
  70. * const eoLocale = require('date-fns/locale/eo')
  71. * const result = formatDistanceToNowStrict(
  72. * new Date(2016, 0, 1),
  73. * {locale: eoLocale}
  74. * )
  75. * //=> '1 jaro'
  76. */
  77. export function formatDistanceToNowStrict(date, options) {
  78. return formatDistanceStrict(date, constructNow(date), options);
  79. }
  80. // Fallback for modularized imports:
  81. export default formatDistanceToNowStrict;