index.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import differenceInCalendarDays from "../differenceInCalendarDays/index.js";
  2. import format from "../format/index.js";
  3. import defaultLocale from "../locale/en-US/index.js";
  4. import subMilliseconds from "../subMilliseconds/index.js";
  5. import toDate from "../toDate/index.js";
  6. import getTimezoneOffsetInMilliseconds from "../_lib/getTimezoneOffsetInMilliseconds/index.js";
  7. import requiredArgs from "../_lib/requiredArgs/index.js";
  8. /**
  9. * @name formatRelative
  10. * @category Common Helpers
  11. * @summary Represent the date in words relative to the given base date.
  12. *
  13. * @description
  14. * Represent the date in words relative to the given base date.
  15. *
  16. * | Distance to the base date | Result |
  17. * |---------------------------|---------------------------|
  18. * | Previous 6 days | last Sunday at 04:30 AM |
  19. * | Last day | yesterday at 04:30 AM |
  20. * | Same day | today at 04:30 AM |
  21. * | Next day | tomorrow at 04:30 AM |
  22. * | Next 6 days | Sunday at 04:30 AM |
  23. * | Other | 12/31/2017 |
  24. *
  25. * ### v2.0.0 breaking changes:
  26. *
  27. * - [Changes that are common for the whole library](https://github.com/date-fns/date-fns/blob/master/docs/upgradeGuide.md#Common-Changes).
  28. *
  29. * @param {Date|Number} date - the date to format
  30. * @param {Date|Number} baseDate - the date to compare with
  31. * @param {Object} [options] - an object with options.
  32. * @param {Locale} [options.locale=defaultLocale] - the locale object. See [Locale]{@link https://date-fns.org/docs/Locale}
  33. * @param {0|1|2|3|4|5|6} [options.weekStartsOn=0] - the index of the first day of the week (0 - Sunday)
  34. * @returns {String} the date in words
  35. * @throws {TypeError} 2 arguments required
  36. * @throws {RangeError} `date` must not be Invalid Date
  37. * @throws {RangeError} `baseDate` must not be Invalid Date
  38. * @throws {RangeError} `options.weekStartsOn` must be between 0 and 6
  39. * @throws {RangeError} `options.locale` must contain `localize` property
  40. * @throws {RangeError} `options.locale` must contain `formatLong` property
  41. * @throws {RangeError} `options.locale` must contain `formatRelative` property
  42. */
  43. export default function formatRelative(dirtyDate, dirtyBaseDate, dirtyOptions) {
  44. requiredArgs(2, arguments);
  45. var date = toDate(dirtyDate);
  46. var baseDate = toDate(dirtyBaseDate);
  47. var _ref = dirtyOptions || {},
  48. _ref$locale = _ref.locale,
  49. locale = _ref$locale === void 0 ? defaultLocale : _ref$locale,
  50. _ref$weekStartsOn = _ref.weekStartsOn,
  51. weekStartsOn = _ref$weekStartsOn === void 0 ? 0 : _ref$weekStartsOn;
  52. if (!locale.localize) {
  53. throw new RangeError('locale must contain localize property');
  54. }
  55. if (!locale.formatLong) {
  56. throw new RangeError('locale must contain formatLong property');
  57. }
  58. if (!locale.formatRelative) {
  59. throw new RangeError('locale must contain formatRelative property');
  60. }
  61. var diff = differenceInCalendarDays(date, baseDate);
  62. if (isNaN(diff)) {
  63. throw new RangeError('Invalid time value');
  64. }
  65. var token;
  66. if (diff < -6) {
  67. token = 'other';
  68. } else if (diff < -1) {
  69. token = 'lastWeek';
  70. } else if (diff < 0) {
  71. token = 'yesterday';
  72. } else if (diff < 1) {
  73. token = 'today';
  74. } else if (diff < 2) {
  75. token = 'tomorrow';
  76. } else if (diff < 7) {
  77. token = 'nextWeek';
  78. } else {
  79. token = 'other';
  80. }
  81. var utcDate = subMilliseconds(date, getTimezoneOffsetInMilliseconds(date));
  82. var utcBaseDate = subMilliseconds(baseDate, getTimezoneOffsetInMilliseconds(baseDate));
  83. var formatStr = locale.formatRelative(token, utcDate, utcBaseDate, {
  84. locale: locale,
  85. weekStartsOn: weekStartsOn
  86. });
  87. return format(date, formatStr, {
  88. locale: locale,
  89. weekStartsOn: weekStartsOn
  90. });
  91. }