format.ts 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. import i18n, { t } from "i18next";
  2. const A_DAY = 24 * 60 * 60 * 1000;
  3. const dateTimeFullFormatter1 = new Intl.DateTimeFormat(i18n.language, {
  4. weekday: "short",
  5. month: "short",
  6. day: "numeric",
  7. });
  8. const dateTimeFullFormatter2 = new Intl.DateTimeFormat(i18n.language, {
  9. year: "numeric",
  10. });
  11. const dateTimeShortFormatter = new Intl.DateTimeFormat(i18n.language, {
  12. month: "short",
  13. day: "numeric",
  14. year: "numeric",
  15. hour: "2-digit",
  16. minute: "2-digit",
  17. });
  18. const timeFormatter = new Intl.DateTimeFormat(i18n.language, {
  19. timeStyle: "short",
  20. });
  21. export function formatDateFull(date: number | Date) {
  22. return [dateTimeFullFormatter1, dateTimeFullFormatter2]
  23. .map((f) => f.format(date))
  24. .join(" ");
  25. }
  26. export function formatDate(date: number | Date) {
  27. const withinYear =
  28. new Date().getFullYear() === new Date(date).getFullYear();
  29. const dateTimeFormat2 = !withinYear ? dateTimeFullFormatter2 : null;
  30. return [dateTimeFullFormatter1, dateTimeFormat2]
  31. .filter((f) => !!f)
  32. .map((f) => f.format(date))
  33. .join(" ");
  34. }
  35. export function formatDateTimeShort(date: number | Date) {
  36. return dateTimeShortFormatter.format(date);
  37. }
  38. export function formatTime(date: number | Date) {
  39. return timeFormatter.format(date).toUpperCase();
  40. }
  41. export function formatDateTimeFull(dateTime: number | Date): string {
  42. return [formatDateFull(dateTime), t("at"), formatTime(dateTime)].join(" ");
  43. }
  44. export function formatDateTime(dateTime: number | Date): string {
  45. return [formatDate(dateTime), t("at"), formatTime(dateTime)].join(" ");
  46. }
  47. export function formatDateRelative(date: number) {
  48. const units = {
  49. year: 24 * 60 * 60 * 1000 * 365,
  50. month: (24 * 60 * 60 * 1000 * 365) / 12,
  51. day: 24 * 60 * 60 * 1000,
  52. hour: 60 * 60 * 1000,
  53. minute: 60 * 1000,
  54. second: 1000,
  55. };
  56. const relativeDateFormat = new Intl.RelativeTimeFormat(i18n.language, {
  57. localeMatcher: "best fit",
  58. numeric: "always",
  59. style: "long",
  60. });
  61. const elapsed = date - Date.now(); // "Math.abs" accounts for both "past" & "future" scenarios
  62. for (const u in units)
  63. if (Math.abs(elapsed) > units[u] || u === "second")
  64. return relativeDateFormat.format(
  65. Math.round(elapsed / units[u]),
  66. u as Intl.RelativeTimeFormatUnit,
  67. );
  68. }
  69. export const isSameDay = (first, second) => {
  70. return (
  71. first.getFullYear() === second.getFullYear() &&
  72. first.getMonth() === second.getMonth() &&
  73. first.getDate() === second.getDate()
  74. );
  75. };
  76. export const getDate = (item) => {
  77. const currentDate = item.metadata.creationTime / 1000;
  78. const date = isSameDay(new Date(currentDate), new Date())
  79. ? t('TODAY')
  80. : isSameDay(new Date(currentDate), new Date(Date.now() - A_DAY))
  81. ? t('YESTERDAY')
  82. : formatDate(currentDate);
  83. return date;
  84. };