Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Chat: Mention recent dates of the week by name #608

Merged
merged 1 commit into from
Jan 29, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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