diff --git a/app/javascript/mastodon/utils/time.test.ts b/app/javascript/mastodon/utils/time.test.ts index add0c7d3cd9..fffcd6f5a18 100644 --- a/app/javascript/mastodon/utils/time.test.ts +++ b/app/javascript/mastodon/utils/time.test.ts @@ -1,4 +1,13 @@ -import { DAY, HOUR, MINUTE, relativeTimeParts, SECOND } from './time'; +import { createIntl } from 'react-intl'; + +import { + DAY, + HOUR, + MINUTE, + SECOND, + relativeTimeParts, + formatTime, +} from './time'; describe('relativeTimeParts', () => { const now = Date.now(); @@ -36,3 +45,19 @@ describe('relativeTimeParts', () => { expect(relativeTimeParts(now + input, now)).toMatchObject(expected); }); }); + +describe('formatTime with day-only timestamps', () => { + const intl = createIntl({ locale: 'en' }); + + const now = Date.parse('2026-08-20T22:56:00Z'); + + test.concurrent.each([ + ['2026-08-20', 'today'], + ['2026-08-19', '1 day ago'], + ['2026-08-17', '3 days ago'], + ])('should format the day-only value %s as "%s"', (date, expected) => { + expect( + formatTime({ timestamp: Date.parse(date), intl, now, noTime: true }), + ).toBe(expected); + }); +}); diff --git a/app/javascript/mastodon/utils/time.ts b/app/javascript/mastodon/utils/time.ts index c2bbdc747f7..d22cf4940d6 100644 --- a/app/javascript/mastodon/utils/time.ts +++ b/app/javascript/mastodon/utils/time.ts @@ -41,13 +41,13 @@ export function relativeTimeParts( return { value: sign * Math.floor(absDelta / DAY), unit: 'day', delta }; } -export function isToday(ts: number, now = Date.now()): boolean { +export function isSameUTCDay(ts: number, now = Date.now()): boolean { const date = new Date(ts); const nowDate = new Date(now); return ( - date.getDate() === nowDate.getDate() && - date.getMonth() === nowDate.getMonth() && - date.getFullYear() === nowDate.getFullYear() + date.getUTCDate() === nowDate.getUTCDate() && + date.getUTCMonth() === nowDate.getUTCMonth() && + date.getUTCFullYear() === nowDate.getUTCFullYear() ); } @@ -138,7 +138,7 @@ export function formatTime({ const { value, unit } = relativeTimeParts(timestamp, now); // If we're only showing days, show "today" for the current day. - if (noTime && isToday(timestamp, now)) { + if (noTime && isSameUTCDay(timestamp, now)) { return intl.formatMessage(timeMessages.today); }