= ({ post, morePosts, preview }) => {
{" "}
- · {formatter.format(new Date(post.date))}
+ · {formatDate(new Date(post.date))}
diff --git a/app/core/helpers/date-formatter.ts b/app/core/helpers/date-formatter.ts
new file mode 100644
index 0000000..56a525a
--- /dev/null
+++ b/app/core/helpers/date-formatter.ts
@@ -0,0 +1,48 @@
+import { DateTime } from "luxon";
+
+export function formatDate(date: Date, config?: Intl.DateTimeFormatOptions): string {
+ const dateFormatter = Intl.DateTimeFormat(
+ "en-US",
+ config ?? {
+ day: "2-digit",
+ month: "short",
+ year: "numeric",
+ },
+ );
+
+ return dateFormatter.format(date);
+}
+
+export function formatTime(date: Date, config?: Intl.DateTimeFormatOptions): string {
+ const timeFormatter = Intl.DateTimeFormat(
+ "en-US",
+ config ?? {
+ hour: "2-digit",
+ minute: "2-digit",
+ },
+ );
+
+ return timeFormatter.format(date);
+}
+
+export function formatRelativeDate(date: Date): string {
+ const dateTime = DateTime.fromJSDate(date);
+ const diff = dateTime.diffNow("days");
+
+ const isToday = diff.days >= -1;
+ if (isToday) {
+ return dateTime.toFormat("HH:mm", { locale: "en-US" });
+ }
+
+ const isYesterday = diff.days >= -2;
+ if (isYesterday) {
+ return "Yesterday";
+ }
+
+ const isDuringLastWeek = diff.days >= -7;
+ if (isDuringLastWeek) {
+ return dateTime.weekdayLong;
+ }
+
+ return dateTime.toFormat("dd/MM/yyyy", { locale: "en-US" });
+}
diff --git a/app/messages/components/conversation.tsx b/app/messages/components/conversation.tsx
index d12f989..42e4f3a 100644
--- a/app/messages/components/conversation.tsx
+++ b/app/messages/components/conversation.tsx
@@ -5,6 +5,7 @@ import clsx from "clsx";
import { Direction } from "../../../db";
import useConversation from "../hooks/use-conversation";
import NewMessageArea from "./new-message-area";
+import { formatDate, formatTime } from "../../core/helpers/date-formatter";
export default function Conversation() {
const router = useRouter();
@@ -42,18 +43,13 @@ export default function Conversation() {
{(!isPreviousMessageFromSameSender || !shouldGroupMessages) && (
- {new Date(message.sentAt).toLocaleDateString("en-US", {
+ {formatDate(new Date(message.sentAt), {
weekday: "long",
day: "2-digit",
month: "short",
})}
-
- {new Date(message.sentAt).toLocaleTimeString("en-US", {
- hour: "2-digit",
- minute: "2-digit",
- })}
-
+ {formatTime(new Date(message.sentAt))}
)}
diff --git a/app/messages/components/conversations-list.tsx b/app/messages/components/conversations-list.tsx
index 8450c18..0529d27 100644
--- a/app/messages/components/conversations-list.tsx
+++ b/app/messages/components/conversations-list.tsx
@@ -1,9 +1,9 @@
import { Link, useQuery, Routes } from "blitz";
-import { DateTime } from "luxon";
import { faChevronRight } from "@fortawesome/pro-regular-svg-icons";
import { FontAwesomeIcon } from "@fortawesome/react-fontawesome";
import getConversationsQuery from "../queries/get-conversations";
+import { formatRelativeDate } from "../../core/helpers/date-formatter";
export default function ConversationsList() {
const conversations = useQuery(getConversationsQuery, {})[0];
@@ -23,7 +23,7 @@ export default function ConversationsList() {
{formattedPhoneNumber}
- {formatMessageDate(lastMessage.sentAt)}
+ {formatRelativeDate(lastMessage.sentAt)}
@@ -36,20 +36,3 @@ export default function ConversationsList() {
);
}
-
-function formatMessageDate(date: Date): string {
- const messageDate = DateTime.fromJSDate(date);
- const diff = messageDate.diffNow("days");
-
- const isToday = diff.days > -1;
- if (isToday) {
- return messageDate.toFormat("HH:mm", { locale: "en-US" });
- }
-
- const isDuringLastWeek = diff.days > -8;
- if (isDuringLastWeek) {
- return messageDate.weekdayLong;
- }
-
- return messageDate.toFormat("dd/MM/yyyy", { locale: "en-US" });
-}
diff --git a/app/public-area/pages/roadmap.tsx b/app/public-area/pages/roadmap.tsx
index 6b68e89..6d1de40 100644
--- a/app/public-area/pages/roadmap.tsx
+++ b/app/public-area/pages/roadmap.tsx
@@ -1,10 +1,9 @@
import type { BlitzPage } from "blitz";
-import { Head } from "blitz";
import clsx from "clsx";
import { CheckIcon, XIcon, TerminalIcon } from "@heroicons/react/solid";
-import Header from "../components/header";
-import Footer from "../components/footer";
+import { formatDate } from "../../core/helpers/date-formatter";
+
import Layout from "../components/layout";
const Roadmap: BlitzPage = () => {
@@ -45,7 +44,7 @@ const Roadmap: BlitzPage = () => {
{isDone ? (
-
+
) : null}
@@ -131,12 +130,6 @@ const roadmap: RoadmapItem[] = [
},
];
-const formatter = Intl.DateTimeFormat("en-US", {
- day: "2-digit",
- month: "short",
- year: "numeric",
-});
-
Roadmap.getLayout = (page) => {page};
Roadmap.suppressFirstRenderFlicker = true;