From 16c2e0ce1f337353c84a359e5c6843ba75e28419 Mon Sep 17 00:00:00 2001 From: duogenesis <136373989+duogenesis@users.noreply.github.com> Date: Wed, 29 Jan 2025 19:54:12 +1100 Subject: [PATCH] Chat: Mention recent dates of the week by name (#608) --- util/util.tsx | 20 ++++++++++++++++---- 1 file changed, 16 insertions(+), 4 deletions(-) diff --git a/util/util.tsx b/util/util.tsx index 2fc169a..f3c8788 100644 --- a/util/util.tsx +++ b/util/util.tsx @@ -3,13 +3,14 @@ import { Platform, } from 'react-native'; import { + differenceInCalendarDays, format, formatDistanceToNow, isThisWeek, isThisYear, isToday, - subSeconds, isYesterday, + subSeconds, } from 'date-fns' import _ from 'lodash'; @@ -70,12 +71,23 @@ const friendlyDate = (date: Date): string => { if (isToday(date)) { return 'Today'; } - + if (isYesterday(date)) { return 'Yesterday'; } - - return format(date, 'PPP'); // Makes it use the default locale + + // Check if the date is within the last 7 days + if (differenceInCalendarDays(new Date(), date) < 7) { + return new Intl.DateTimeFormat(undefined, { + weekday: 'long' + }).format(date); + } + + return new Intl.DateTimeFormat(undefined, { + month: 'short', + day: 'numeric', + ...(isThisYear(date) ? {} : { year: 'numeric' }), + }).format(date); }; const delay = (ms: number) => new Promise((r) => setTimeout(r, ms));