Skip to content

Open-source personal finance manager. Self-hosted, privacy-first.

fix(frontend): align calendar weekday headers with the dates below them (#493)

Danny Feliz56a883a93f37
diff --git a/frontend/src/components/transaction-calendar-view.tsx b/frontend/src/components/transaction-calendar-view.tsx
index 19959118..4c382797 100644
--- a/frontend/src/components/transaction-calendar-view.tsx
+++ b/frontend/src/components/transaction-calendar-view.tsx
@@ -6,6 +6,7 @@ import type { TransactionCalendarDay, TransactionCalendarItem, TransactionCalend
import { Skeleton } from '@/components/ui/skeleton'
import { CategoryIcon } from '@/components/category-icon'
import { activityChartData, dayActivity } from '@/lib/calendar-activity'
+import { weekdayShortLabels } from '@/lib/date-utils'
import { cn } from '@/lib/utils'
function parseLocalDate(value: string) {
@@ -103,13 +104,7 @@ export function TransactionCalendarView({
}, [calendar, onSelectedDateChange, selectedDate])
const selectedDay = calendar?.days.find((day) => day.date === selectedDate)
- const weekDays = useMemo(
- () => Array.from({ length: 7 }, (_, index) => {
- const date = new Date(Date.UTC(2024, 0, 7 + index)) // Sunday-start reference week
- return date.toLocaleDateString(dateLocale, { weekday: 'short' })
- }),
- [dateLocale],
- )
+ const weekDays = useMemo(() => weekdayShortLabels(dateLocale), [dateLocale])
if (isLoading) {
return (
diff --git a/frontend/src/lib/date-utils.test.ts b/frontend/src/lib/date-utils.test.ts
index 31f8e26c..4518b3df 100644
--- a/frontend/src/lib/date-utils.test.ts
+++ b/frontend/src/lib/date-utils.test.ts
@@ -1,6 +1,6 @@
import { describe, expect, it } from 'vitest'
-import { localDateString } from './date-utils'
+import { localDateString, weekdayShortLabels } from './date-utils'
describe('localDateString', () => {
it('formats the browser-local calendar day', () => {
@@ -15,3 +15,24 @@ describe('localDateString', () => {
}
})
})
+
+describe('weekdayShortLabels', () => {
+ it('starts the week on Sunday regardless of the viewer timezone', () => {
+ const originalTimezone = process.env.TZ
+
+ try {
+ process.env.TZ = 'America/Santo_Domingo'
+ expect(weekdayShortLabels('en-US')).toEqual(['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'])
+
+ process.env.TZ = 'Pacific/Kiritimati'
+ expect(weekdayShortLabels('en-US')).toEqual(['Sun', 'Mon', 'Tue', 'Wed', 'Thu', 'Fri', 'Sat'])
+ } finally {
+ if (originalTimezone === undefined) delete process.env.TZ
+ else process.env.TZ = originalTimezone
+ }
+ })
+
+ it('translates the labels for the requested locale', () => {
+ expect(weekdayShortLabels('es-ES')[0]).toMatch(/^dom/i)
+ })
+})
diff --git a/frontend/src/lib/date-utils.ts b/frontend/src/lib/date-utils.ts
index 035a53c9..3c27ecce 100644
--- a/frontend/src/lib/date-utils.ts
+++ b/frontend/src/lib/date-utils.ts
@@ -3,3 +3,14 @@ import { format } from 'date-fns'
export function localDateString(date = new Date()) {
return format(date, 'yyyy-MM-dd')
}
+
+// Short weekday names for a Sunday-start calendar header. The reference week is
+// anchored in UTC, so it has to be formatted in UTC as well — otherwise a viewer
+// behind UTC reads each instant as the previous day and every label shifts one
+// column, leaving the headers out of step with the dates underneath them.
+export function weekdayShortLabels(locale: string) {
+ return Array.from({ length: 7 }, (_, index) => {
+ const date = new Date(Date.UTC(2024, 0, 7 + index)) // 2024-01-07 is a Sunday
+ return date.toLocaleDateString(locale, { weekday: 'short', timeZone: 'UTC' })
+ })
+}