Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 9 additions & 2 deletions frontend/src/utils/date.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,15 @@ export function parseDateSafe(rawValue: string | null | undefined): Date | null
: [`${isoCompatible}Z`, isoCompatible, raw]

for (const candidate of candidates) {
const d = new Date(candidate)
if (!Number.isNaN(d.getTime())) return d
const d = new Date(candidate)

// 1. Check if the date is technically valid
const isValid = !Number.isNaN(d.getTime())

// 2. Sanity check: Ensure the year is between 1900 and 2100
const isRealistic = isValid && d.getFullYear() > 1900 && d.getFullYear() < 2100

if (isRealistic) return d
}
} catch (error) {
console.error('Date parsing failed:', error, raw)
Expand Down
18 changes: 18 additions & 0 deletions frontend/testing/unit/utils/date.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,4 +96,22 @@ import {
expect(result.time).not.toBe("UNKNOWN TIME");
});
});
});

describe("Issue #107: Invalid Date Handling", () => {
test("returns N/A for completely random strings", () => {
// This should fail initially because the current function
// might return 'Invalid Date' or crash instead of 'N/A'
expect(formatLocaleDate("not-a-date")).toBe("N/A");
});

test("returns N/A for impossible calendar dates", () => {
// This catches dates that JavaScript usually 'overflows'
expect(formatLocaleDate("2026-13-45")).toBe("N/A");
});

test("returns N/A for numeric strings that aren't timestamps", () => {
// Prevents random 5-digit strings from being parsed as years
expect(formatLocaleDate("99999")).toBe("N/A");
});
});
Loading