formatRelative.mjs 3.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. import { differenceInCalendarDays } from "./differenceInCalendarDays.mjs";
  2. import { format } from "./format.mjs";
  3. import { toDate } from "./toDate.mjs";
  4. import { defaultLocale } from "./_lib/defaultLocale.mjs";
  5. import { getDefaultOptions } from "./_lib/defaultOptions.mjs";
  6. /**
  7. * The {@link formatRelative} function options.
  8. */
  9. /**
  10. * @name formatRelative
  11. * @category Common Helpers
  12. * @summary Represent the date in words relative to the given base date.
  13. *
  14. * @description
  15. * Represent the date in words relative to the given base date.
  16. *
  17. * | Distance to the base date | Result |
  18. * |---------------------------|---------------------------|
  19. * | Previous 6 days | last Sunday at 04:30 AM |
  20. * | Last day | yesterday at 04:30 AM |
  21. * | Same day | today at 04:30 AM |
  22. * | Next day | tomorrow at 04:30 AM |
  23. * | Next 6 days | Sunday at 04:30 AM |
  24. * | Other | 12/31/2017 |
  25. *
  26. * @typeParam DateType - The `Date` type, the function operates on. Gets inferred from passed arguments. Allows to use extensions like [`UTCDate`](https://github.com/date-fns/utc).
  27. *
  28. * @param date - The date to format
  29. * @param baseDate - The date to compare with
  30. * @param options - An object with options
  31. *
  32. * @returns The date in words
  33. *
  34. * @throws `date` must not be Invalid Date
  35. * @throws `baseDate` must not be Invalid Date
  36. * @throws `options.locale` must contain `localize` property
  37. * @throws `options.locale` must contain `formatLong` property
  38. * @throws `options.locale` must contain `formatRelative` property
  39. *
  40. * @example
  41. * // Represent the date of 6 days ago in words relative to the given base date. In this example, today is Wednesday
  42. * const result = formatRelative(subDays(new Date(), 6), new Date())
  43. * //=> "last Thursday at 12:45 AM"
  44. */
  45. export function formatRelative(date, baseDate, options) {
  46. const _date = toDate(date);
  47. const _baseDate = toDate(baseDate);
  48. const defaultOptions = getDefaultOptions();
  49. const locale = options?.locale ?? defaultOptions.locale ?? defaultLocale;
  50. const weekStartsOn =
  51. options?.weekStartsOn ??
  52. options?.locale?.options?.weekStartsOn ??
  53. defaultOptions.weekStartsOn ??
  54. defaultOptions.locale?.options?.weekStartsOn ??
  55. 0;
  56. const diff = differenceInCalendarDays(_date, _baseDate);
  57. if (isNaN(diff)) {
  58. throw new RangeError("Invalid time value");
  59. }
  60. let token;
  61. if (diff < -6) {
  62. token = "other";
  63. } else if (diff < -1) {
  64. token = "lastWeek";
  65. } else if (diff < 0) {
  66. token = "yesterday";
  67. } else if (diff < 1) {
  68. token = "today";
  69. } else if (diff < 2) {
  70. token = "tomorrow";
  71. } else if (diff < 7) {
  72. token = "nextWeek";
  73. } else {
  74. token = "other";
  75. }
  76. const formatStr = locale.formatRelative(token, _date, _baseDate, {
  77. locale,
  78. weekStartsOn,
  79. });
  80. return format(_date, formatStr, { locale, weekStartsOn });
  81. }
  82. // Fallback for modularized imports:
  83. export default formatRelative;