Skip to content

Commit

Permalink
Display relative timestamp for threads on the same calendar day (#10399)
Browse files Browse the repository at this point in the history
* add test cases for <24 different day time formatting

* use day instead of <24h ago to format relative date
  • Loading branch information
Kerry committed Mar 27, 2023
1 parent 68fa9ae commit cd700e2
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 2 deletions.
6 changes: 5 additions & 1 deletion src/DateUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,10 @@ function withinPast24Hours(prevDate: Date, nextDate: Date): boolean {
return Math.abs(prevDate.getTime() - nextDate.getTime()) <= MILLIS_IN_DAY;
}

function withinCurrentDay(prevDate: Date, nextDate: Date): boolean {
return withinPast24Hours(prevDate, nextDate) && prevDate.getDay() === nextDate.getDay();
}

function withinCurrentYear(prevDate: Date, nextDate: Date): boolean {
return prevDate.getFullYear() === nextDate.getFullYear();
}
Expand Down Expand Up @@ -229,7 +233,7 @@ export function formatFullDateNoDayNoTime(date: Date): string {

export function formatRelativeTime(date: Date, showTwelveHour = false): string {
const now = new Date(Date.now());
if (withinPast24Hours(date, now)) {
if (withinCurrentDay(date, now)) {
return formatTime(date, showTwelveHour);
} else {
const months = getMonthsArray();
Expand Down
9 changes: 8 additions & 1 deletion test/utils/DateUtils-test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,18 @@ describe("formatRelativeTime", () => {
dateSpy.mockRestore();
});

it("returns hour format for events created less than 24 hours ago", () => {
it("returns hour format for events created in the same day", () => {
// Tuesday, 2 November 2021 11:01:00 UTC
const date = new Date(2021, 10, 2, 11, 1, 23, 0);
expect(formatRelativeTime(date)).toBe("11:01");
});

it("returns month and day for events created less than 24h ago but on a different day", () => {
// Monday, 1 November 2021 23:01:00 UTC
const date = new Date(2021, 10, 1, 23, 1, 23, 0);
expect(formatRelativeTime(date)).toBe("Nov 1");
});

it("honours the hour format setting", () => {
const date = new Date(2021, 10, 2, 11, 1, 23, 0);
expect(formatRelativeTime(date)).toBe("11:01");
Expand Down

0 comments on commit cd700e2

Please sign in to comment.