index.js 3.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. var distanceInWords = require('../distance_in_words/index.js')
  2. /**
  3. * @category Common Helpers
  4. * @summary Return the distance between the given date and now in words.
  5. *
  6. * @description
  7. * Return the distance between the given date and now in words.
  8. *
  9. * | Distance to now | Result |
  10. * |-------------------------------------------------------------------|---------------------|
  11. * | 0 ... 30 secs | less than a minute |
  12. * | 30 secs ... 1 min 30 secs | 1 minute |
  13. * | 1 min 30 secs ... 44 mins 30 secs | [2..44] minutes |
  14. * | 44 mins ... 30 secs ... 89 mins 30 secs | about 1 hour |
  15. * | 89 mins 30 secs ... 23 hrs 59 mins 30 secs | about [2..24] hours |
  16. * | 23 hrs 59 mins 30 secs ... 41 hrs 59 mins 30 secs | 1 day |
  17. * | 41 hrs 59 mins 30 secs ... 29 days 23 hrs 59 mins 30 secs | [2..30] days |
  18. * | 29 days 23 hrs 59 mins 30 secs ... 44 days 23 hrs 59 mins 30 secs | about 1 month |
  19. * | 44 days 23 hrs 59 mins 30 secs ... 59 days 23 hrs 59 mins 30 secs | about 2 months |
  20. * | 59 days 23 hrs 59 mins 30 secs ... 1 yr | [2..12] months |
  21. * | 1 yr ... 1 yr 3 months | about 1 year |
  22. * | 1 yr 3 months ... 1 yr 9 month s | over 1 year |
  23. * | 1 yr 9 months ... 2 yrs | almost 2 years |
  24. * | N yrs ... N yrs 3 months | about N years |
  25. * | N yrs 3 months ... N yrs 9 months | over N years |
  26. * | N yrs 9 months ... N+1 yrs | almost N+1 years |
  27. *
  28. * With `options.includeSeconds == true`:
  29. * | Distance to now | Result |
  30. * |---------------------|----------------------|
  31. * | 0 secs ... 5 secs | less than 5 seconds |
  32. * | 5 secs ... 10 secs | less than 10 seconds |
  33. * | 10 secs ... 20 secs | less than 20 seconds |
  34. * | 20 secs ... 40 secs | half a minute |
  35. * | 40 secs ... 60 secs | less than a minute |
  36. * | 60 secs ... 90 secs | 1 minute |
  37. *
  38. * @param {Date|String|Number} date - the given date
  39. * @param {Object} [options] - the object with options
  40. * @param {Boolean} [options.includeSeconds=false] - distances less than a minute are more detailed
  41. * @param {Boolean} [options.addSuffix=false] - result specifies if the second date is earlier or later than the first
  42. * @param {Object} [options.locale=enLocale] - the locale object
  43. * @returns {String} the distance in words
  44. *
  45. * @example
  46. * // If today is 1 January 2015, what is the distance to 2 July 2014?
  47. * var result = distanceInWordsToNow(
  48. * new Date(2014, 6, 2)
  49. * )
  50. * //=> '6 months'
  51. *
  52. * @example
  53. * // If now is 1 January 2015 00:00:00,
  54. * // what is the distance to 1 January 2015 00:00:15, including seconds?
  55. * var result = distanceInWordsToNow(
  56. * new Date(2015, 0, 1, 0, 0, 15),
  57. * {includeSeconds: true}
  58. * )
  59. * //=> 'less than 20 seconds'
  60. *
  61. * @example
  62. * // If today is 1 January 2015,
  63. * // what is the distance to 1 January 2016, with a suffix?
  64. * var result = distanceInWordsToNow(
  65. * new Date(2016, 0, 1),
  66. * {addSuffix: true}
  67. * )
  68. * //=> 'in about 1 year'
  69. *
  70. * @example
  71. * // If today is 1 January 2015,
  72. * // what is the distance to 1 August 2016 in Esperanto?
  73. * var eoLocale = require('date-fns/locale/eo')
  74. * var result = distanceInWordsToNow(
  75. * new Date(2016, 7, 1),
  76. * {locale: eoLocale}
  77. * )
  78. * //=> 'pli ol 1 jaro'
  79. */
  80. function distanceInWordsToNow (dirtyDate, dirtyOptions) {
  81. return distanceInWords(Date.now(), dirtyDate, dirtyOptions)
  82. }
  83. module.exports = distanceInWordsToNow