diff --git a/src/utils/date.test.ts b/src/utils/date.test.ts new file mode 100644 index 0000000..55a880d --- /dev/null +++ b/src/utils/date.test.ts @@ -0,0 +1,12 @@ +import { formatLeaderboardTimestamp } from './date'; + +describe('Leaderboard Timestamp Utilities', () => { + it('should return empty string for empty input', () => { + expect(formatLeaderboardTimestamp('')).toBe(''); + }); + + it('should format valid timestamp', () => { + const res = formatLeaderboardTimestamp('2026-08-06T12:00:00Z'); + expect(res).toBeTruthy(); + }); +}); diff --git a/src/utils/date.ts b/src/utils/date.ts new file mode 100644 index 0000000..2ff81f2 --- /dev/null +++ b/src/utils/date.ts @@ -0,0 +1,13 @@ +export function formatLeaderboardTimestamp(timestamp: string | number | Date): string { + if (!timestamp) return ''; + try { + const date = new Date(timestamp); + if (isNaN(date.getTime())) return ''; + return new Intl.DateTimeFormat(navigator.language || 'en-US', { + dateStyle: 'medium', + timeStyle: 'short' + }).format(date); + } catch (err) { + return new Date(timestamp).toISOString(); + } +}