Skip to content

Commit c120876

Browse files
fix(web): relative days follow the formatter's time zone
Co-Authored-By: Claude Opus 5.5 <[email protected]>
1 parent 948416f commit c120876

2 files changed

Lines changed: 53 additions & 13 deletions

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
import { describe, expect, it } from "vite-plus/test";
2+
3+
import { formatDayAwareTimestamp, formatUpcomingTimestamp } from "./timestampFormat";
4+
5+
describe("timestamps in an explicit time zone", () => {
6+
// 23:00 UTC on July 1 is 7 PM on July 1 in New York, whatever the host zone.
7+
const now = Date.parse("2026-07-01T23:00:00.000Z");
8+
const zone = "America/New_York";
9+
10+
it("picks relative days from the calendar in that zone", () => {
11+
expect(formatUpcomingTimestamp("2026-07-02T00:30:00.000Z", "12-hour", now, zone)).not.toMatch(
12+
/tomorrow/,
13+
);
14+
expect(formatUpcomingTimestamp("2026-07-02T05:30:00.000Z", "12-hour", now, zone)).toMatch(
15+
/^tomorrow at /,
16+
);
17+
expect(formatDayAwareTimestamp("2026-07-01T03:30:00.000Z", "12-hour", now, zone)).toMatch(
18+
/^yesterday at /,
19+
);
20+
});
21+
});

‎apps/web/src/timestampFormat.ts‎

Lines changed: 32 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -148,8 +148,30 @@ const numericDateWithYearFormatter = new Intl.DateTimeFormat(
148148
numericDateWithYearOptions,
149149
);
150150

151-
function formatNumericDate(date: Date, now: Date, timeZone: string | undefined): string {
152-
const sameYear = date.getFullYear() === now.getFullYear();
151+
interface CalendarDay {
152+
year: number;
153+
/** Days since the epoch, so differences are whole days across DST shifts. */
154+
index: number;
155+
}
156+
157+
function calendarDay(date: Date, timeZone: string | undefined): CalendarDay {
158+
if (!timeZone) {
159+
const year = date.getFullYear();
160+
return { year, index: Date.UTC(year, date.getMonth(), date.getDate()) / 86_400_000 };
161+
}
162+
const parts = new Intl.DateTimeFormat("en-US", {
163+
timeZone,
164+
year: "numeric",
165+
month: "numeric",
166+
day: "numeric",
167+
}).formatToParts(date);
168+
const part = (type: Intl.DateTimeFormatPartTypes) =>
169+
Number(parts.find((entry) => entry.type === type)?.value);
170+
const year = part("year");
171+
return { year, index: Date.UTC(year, part("month") - 1, part("day")) / 86_400_000 };
172+
}
173+
174+
function formatNumericDate(date: Date, sameYear: boolean, timeZone: string | undefined): string {
153175
if (!timeZone) {
154176
return (sameYear ? numericDateFormatter : numericDateWithYearFormatter).format(date);
155177
}
@@ -169,15 +191,13 @@ export function formatDayAwareTimestamp(
169191
if (!date) return "";
170192
const time = getTimestampFormatter(timestampFormat, false, timeZone).format(date);
171193

172-
const now = new Date(nowMs);
173-
const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
174-
const startOfMessageDay = new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
175-
// Round so DST-shifted 23/25 hour days still count as whole days.
176-
const dayDiff = Math.round((startOfToday - startOfMessageDay) / 86_400_000);
194+
const today = calendarDay(new Date(nowMs), timeZone);
195+
const messageDay = calendarDay(date, timeZone);
196+
const dayDiff = today.index - messageDay.index;
177197

178198
if (dayDiff <= 0) return time;
179199
if (dayDiff === 1) return `yesterday at ${time}`;
180-
return `${formatNumericDate(date, now, timeZone)} ${time}`;
200+
return `${formatNumericDate(date, messageDay.year === today.year, timeZone)} ${time}`;
181201
}
182202

183203
/** `timeZone` overrides the zone the formatters cached when they were built. */
@@ -191,15 +211,14 @@ export function formatUpcomingTimestamp(
191211
if (!date) return "";
192212
const time = getTimestampFormatter(timestampFormat, false, timeZone).format(date);
193213

194-
const now = new Date(nowMs);
195-
const startOfToday = new Date(now.getFullYear(), now.getMonth(), now.getDate()).getTime();
196-
const startOfTargetDay = new Date(date.getFullYear(), date.getMonth(), date.getDate()).getTime();
197-
const dayDiff = Math.round((startOfTargetDay - startOfToday) / 86_400_000);
214+
const today = calendarDay(new Date(nowMs), timeZone);
215+
const targetDay = calendarDay(date, timeZone);
216+
const dayDiff = targetDay.index - today.index;
198217

199218
if (dayDiff < 0) return formatDayAwareTimestamp(isoDate, timestampFormat, nowMs, timeZone);
200219
if (dayDiff === 0) return time;
201220
if (dayDiff === 1) return `tomorrow at ${time}`;
202-
return `${formatNumericDate(date, now, timeZone)} ${time}`;
221+
return `${formatNumericDate(date, targetDay.year === today.year, timeZone)} ${time}`;
203222
}
204223

205224
type RelativeTimeParts = { value: string; suffix: string | null };

0 commit comments

Comments
 (0)