Skip to content

Commit

Permalink
Chat: Mention recent dates of the week by name (#608)
Browse files Browse the repository at this point in the history
  • Loading branch information
duogenesis authored Jan 29, 2025
1 parent 462598f commit 16c2e0c
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions util/util.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -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));
Expand Down

0 comments on commit 16c2e0c

Please sign in to comment.