index.js 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203
  1. var compareDesc = require('../compare_desc/index.js')
  2. var parse = require('../parse/index.js')
  3. var differenceInSeconds = require('../difference_in_seconds/index.js')
  4. var differenceInMonths = require('../difference_in_months/index.js')
  5. var enLocale = require('../locale/en/index.js')
  6. var MINUTES_IN_DAY = 1440
  7. var MINUTES_IN_ALMOST_TWO_DAYS = 2520
  8. var MINUTES_IN_MONTH = 43200
  9. var MINUTES_IN_TWO_MONTHS = 86400
  10. /**
  11. * @category Common Helpers
  12. * @summary Return the distance between the given dates in words.
  13. *
  14. * @description
  15. * Return the distance between the given dates in words.
  16. *
  17. * | Distance between dates | 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 between dates | 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. * @param {Date|String|Number} dateToCompare - the date to compare with
  47. * @param {Date|String|Number} date - the other date
  48. * @param {Object} [options] - the object with options
  49. * @param {Boolean} [options.includeSeconds=false] - distances less than a minute are more detailed
  50. * @param {Boolean} [options.addSuffix=false] - result indicates if the second date is earlier or later than the first
  51. * @param {Object} [options.locale=enLocale] - the locale object
  52. * @returns {String} the distance in words
  53. *
  54. * @example
  55. * // What is the distance between 2 July 2014 and 1 January 2015?
  56. * var result = distanceInWords(
  57. * new Date(2014, 6, 2),
  58. * new Date(2015, 0, 1)
  59. * )
  60. * //=> '6 months'
  61. *
  62. * @example
  63. * // What is the distance between 1 January 2015 00:00:15
  64. * // and 1 January 2015 00:00:00, including seconds?
  65. * var result = distanceInWords(
  66. * new Date(2015, 0, 1, 0, 0, 15),
  67. * new Date(2015, 0, 1, 0, 0, 0),
  68. * {includeSeconds: true}
  69. * )
  70. * //=> 'less than 20 seconds'
  71. *
  72. * @example
  73. * // What is the distance from 1 January 2016
  74. * // to 1 January 2015, with a suffix?
  75. * var result = distanceInWords(
  76. * new Date(2016, 0, 1),
  77. * new Date(2015, 0, 1),
  78. * {addSuffix: true}
  79. * )
  80. * //=> 'about 1 year ago'
  81. *
  82. * @example
  83. * // What is the distance between 1 August 2016 and 1 January 2015 in Esperanto?
  84. * var eoLocale = require('date-fns/locale/eo')
  85. * var result = distanceInWords(
  86. * new Date(2016, 7, 1),
  87. * new Date(2015, 0, 1),
  88. * {locale: eoLocale}
  89. * )
  90. * //=> 'pli ol 1 jaro'
  91. */
  92. function distanceInWords (dirtyDateToCompare, dirtyDate, dirtyOptions) {
  93. var options = dirtyOptions || {}
  94. var comparison = compareDesc(dirtyDateToCompare, dirtyDate)
  95. var locale = options.locale
  96. var localize = enLocale.distanceInWords.localize
  97. if (locale && locale.distanceInWords && locale.distanceInWords.localize) {
  98. localize = locale.distanceInWords.localize
  99. }
  100. var localizeOptions = {
  101. addSuffix: Boolean(options.addSuffix),
  102. comparison: comparison
  103. }
  104. var dateLeft, dateRight
  105. if (comparison > 0) {
  106. dateLeft = parse(dirtyDateToCompare)
  107. dateRight = parse(dirtyDate)
  108. } else {
  109. dateLeft = parse(dirtyDate)
  110. dateRight = parse(dirtyDateToCompare)
  111. }
  112. var seconds = differenceInSeconds(dateRight, dateLeft)
  113. var offset = dateRight.getTimezoneOffset() - dateLeft.getTimezoneOffset()
  114. var minutes = Math.round(seconds / 60) - offset
  115. var months
  116. // 0 up to 2 mins
  117. if (minutes < 2) {
  118. if (options.includeSeconds) {
  119. if (seconds < 5) {
  120. return localize('lessThanXSeconds', 5, localizeOptions)
  121. } else if (seconds < 10) {
  122. return localize('lessThanXSeconds', 10, localizeOptions)
  123. } else if (seconds < 20) {
  124. return localize('lessThanXSeconds', 20, localizeOptions)
  125. } else if (seconds < 40) {
  126. return localize('halfAMinute', null, localizeOptions)
  127. } else if (seconds < 60) {
  128. return localize('lessThanXMinutes', 1, localizeOptions)
  129. } else {
  130. return localize('xMinutes', 1, localizeOptions)
  131. }
  132. } else {
  133. if (minutes === 0) {
  134. return localize('lessThanXMinutes', 1, localizeOptions)
  135. } else {
  136. return localize('xMinutes', minutes, localizeOptions)
  137. }
  138. }
  139. // 2 mins up to 0.75 hrs
  140. } else if (minutes < 45) {
  141. return localize('xMinutes', minutes, localizeOptions)
  142. // 0.75 hrs up to 1.5 hrs
  143. } else if (minutes < 90) {
  144. return localize('aboutXHours', 1, localizeOptions)
  145. // 1.5 hrs up to 24 hrs
  146. } else if (minutes < MINUTES_IN_DAY) {
  147. var hours = Math.round(minutes / 60)
  148. return localize('aboutXHours', hours, localizeOptions)
  149. // 1 day up to 1.75 days
  150. } else if (minutes < MINUTES_IN_ALMOST_TWO_DAYS) {
  151. return localize('xDays', 1, localizeOptions)
  152. // 1.75 days up to 30 days
  153. } else if (minutes < MINUTES_IN_MONTH) {
  154. var days = Math.round(minutes / MINUTES_IN_DAY)
  155. return localize('xDays', days, localizeOptions)
  156. // 1 month up to 2 months
  157. } else if (minutes < MINUTES_IN_TWO_MONTHS) {
  158. months = Math.round(minutes / MINUTES_IN_MONTH)
  159. return localize('aboutXMonths', months, localizeOptions)
  160. }
  161. months = differenceInMonths(dateRight, dateLeft)
  162. // 2 months up to 12 months
  163. if (months < 12) {
  164. var nearestMonth = Math.round(minutes / MINUTES_IN_MONTH)
  165. return localize('xMonths', nearestMonth, localizeOptions)
  166. // 1 year up to max Date
  167. } else {
  168. var monthsSinceStartOfYear = months % 12
  169. var years = Math.floor(months / 12)
  170. // N years up to 1 years 3 months
  171. if (monthsSinceStartOfYear < 3) {
  172. return localize('aboutXYears', years, localizeOptions)
  173. // N years 3 months up to N years 9 months
  174. } else if (monthsSinceStartOfYear < 9) {
  175. return localize('overXYears', years, localizeOptions)
  176. // N years 9 months up to N year 12 months
  177. } else {
  178. return localize('almostXYears', years + 1, localizeOptions)
  179. }
  180. }
  181. }
  182. module.exports = distanceInWords