index.js 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. import formatDistanceStrict from "../formatDistanceStrict/index.js";
  2. import requiredArgs from "../_lib/requiredArgs/index.js";
  3. /**
  4. * @name formatDistanceToNowStrict
  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 dates in words, using strict units.
  11. * This is like `formatDistance`, but does not use helpers like 'almost', 'over',
  12. * 'less than' and the like.
  13. *
  14. * | Distance between dates | Result |
  15. * |------------------------|---------------------|
  16. * | 0 ... 59 secs | [0..59] seconds |
  17. * | 1 ... 59 mins | [1..59] minutes |
  18. * | 1 ... 23 hrs | [1..23] hours |
  19. * | 1 ... 29 days | [1..29] days |
  20. * | 1 ... 11 months | [1..11] months |
  21. * | 1 ... N years | [1..N] years |
  22. *
  23. * @param {Date|Number} date - the given date
  24. * @param {Object} [options] - an object with options.
  25. * @param {Boolean} [options.addSuffix=false] - result indicates if the second date is earlier or later than the first
  26. * @param {'second'|'minute'|'hour'|'day'|'month'|'year'} [options.unit] - if specified, will force a unit
  27. * @param {'floor'|'ceil'|'round'} [options.roundingMethod='round'] - which way to round partial units
  28. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  29. * @returns {String} the distance in words
  30. * @throws {TypeError} 1 argument required
  31. * @throws {RangeError} `date` must not be Invalid Date
  32. * @throws {RangeError} `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. * var 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. * var result = formatDistanceToNowStrict(
  45. * new Date(2015, 0, 1, 0, 0, 15)
  46. * )
  47. * //=> '20 seconds'
  48. *
  49. * @example
  50. * // If today is 1 January 2015,
  51. * // what is the distance to 1 January 2016, with a suffix?
  52. * var 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. * var 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 August 2016 in Esperanto?
  70. * var eoLocale = require('date-fns/locale/eo')
  71. * var result = formatDistanceToNowStrict(
  72. * new Date(2016, 7, 1),
  73. * {locale: eoLocale}
  74. * )
  75. * //=> '1 jaro'
  76. */
  77. export default function formatDistanceToNowStrict(dirtyDate, dirtyOptions) {
  78. requiredArgs(1, arguments);
  79. return formatDistanceStrict(dirtyDate, Date.now(), dirtyOptions);
  80. }