formatRelative.js 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. import { defaultLocale } from "./_lib/defaultLocale.js";
  2. import { getDefaultOptions } from "./_lib/defaultOptions.js";
  3. import { normalizeDates } from "./_lib/normalizeDates.js";
  4. import { differenceInCalendarDays } from "./differenceInCalendarDays.js";
  5. import { format } from "./format.js";
  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. * @param date - The date to format
  27. * @param baseDate - The date to compare with
  28. * @param options - An object with options
  29. *
  30. * @returns The date in words
  31. *
  32. * @throws `date` must not be Invalid Date
  33. * @throws `baseDate` must not be Invalid Date
  34. * @throws `options.locale` must contain `localize` property
  35. * @throws `options.locale` must contain `formatLong` property
  36. * @throws `options.locale` must contain `formatRelative` property
  37. *
  38. * @example
  39. * // Represent the date of 6 days ago in words relative to the given base date. In this example, today is Wednesday
  40. * const result = formatRelative(subDays(new Date(), 6), new Date())
  41. * //=> "last Thursday at 12:45 AM"
  42. */
  43. export function formatRelative(date, baseDate, options) {
  44. const [date_, baseDate_] = normalizeDates(options?.in, date, baseDate);
  45. const defaultOptions = getDefaultOptions();
  46. const locale = options?.locale ?? defaultOptions.locale ?? defaultLocale;
  47. const weekStartsOn =
  48. options?.weekStartsOn ??
  49. options?.locale?.options?.weekStartsOn ??
  50. defaultOptions.weekStartsOn ??
  51. defaultOptions.locale?.options?.weekStartsOn ??
  52. 0;
  53. const diff = differenceInCalendarDays(date_, baseDate_);
  54. if (isNaN(diff)) {
  55. throw new RangeError("Invalid time value");
  56. }
  57. let token;
  58. if (diff < -6) {
  59. token = "other";
  60. } else if (diff < -1) {
  61. token = "lastWeek";
  62. } else if (diff < 0) {
  63. token = "yesterday";
  64. } else if (diff < 1) {
  65. token = "today";
  66. } else if (diff < 2) {
  67. token = "tomorrow";
  68. } else if (diff < 7) {
  69. token = "nextWeek";
  70. } else {
  71. token = "other";
  72. }
  73. const formatStr = locale.formatRelative(token, date_, baseDate_, {
  74. locale,
  75. weekStartsOn,
  76. });
  77. return format(date_, formatStr, { locale, weekStartsOn });
  78. }
  79. // Fallback for modularized imports:
  80. export default formatRelative;