Skip to content

Commit beb86f5

Browse files
committed
formatDateCompact use schema, respect timezone
- Use dailyDateSchema parsing instead of regex - If time zone is specified, interpret date as UTC - Add unit tests
1 parent af1f389 commit beb86f5

1 file changed

Lines changed: 11 additions & 4 deletions

File tree

‎src/data-loader.ts‎

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -569,10 +569,11 @@ export function formatDate(dateStr: string, timezone?: string, locale?: string):
569569
export function formatDateCompact(dateStr: string, timezone: string | undefined, locale: string): string {
570570
// For YYYY-MM-DD format, append T00:00:00 to parse as local date
571571
// Without this, new Date('YYYY-MM-DD') interprets as UTC midnight
572-
const localDateStr = /^\d{4}-\d{2}-\d{2}$/.test(dateStr)
573-
? `${dateStr}T00:00:00`
574-
: dateStr;
575-
const date = new Date(localDateStr);
572+
const date = dailyDateSchema.safeParse(dateStr).success
573+
? timezone
574+
? new Date(`${dateStr}T00:00:00Z`)
575+
: new Date(`${dateStr}T00:00:00`)
576+
: new Date(dateStr);
576577
const formatter = createDatePartsFormatter(timezone, locale);
577578
const parts = formatter.formatToParts(date);
578579
const year = parts.find(p => p.type === 'year')?.value ?? '';
@@ -1439,6 +1440,12 @@ if (import.meta.vitest != null) {
14391440

14401441
// Asia/Tokyo timezone (crosses to next day)
14411442
expect(formatDateCompact(testTimestamp, 'Asia/Tokyo', 'en-US')).toBe('2024\n01-02');
1443+
1444+
// Daily date defined as UTC is preserved
1445+
expect(formatDateCompact('2024-01-01', 'UTC', 'en-US')).toBe('2024\n01-01');
1446+
1447+
// Daily date already in local time is preserved instead of being interpreted as UTC
1448+
expect(formatDateCompact('2024-01-01', undefined, 'en-US')).toBe('2024\n01-01');
14421449
});
14431450

14441451
it('handles various date formats', () => {

0 commit comments

Comments
 (0)