diff --git a/frontend/src/hooks/useTrafficData.ts b/frontend/src/hooks/useTrafficData.ts index bbd7b74ef6..f96c8cba01 100644 --- a/frontend/src/hooks/useTrafficData.ts +++ b/frontend/src/hooks/useTrafficData.ts @@ -47,6 +47,18 @@ const calculateTrafficDataCost = (trafficData: number) => { const padMonth = (month: number): string => month < 10 ? `0${month}` : `${month}`; +const safeLocaleString = (date: Date): string => { + try { + return date.toLocaleString('en-US', { + day: '2-digit', + month: '2-digit', + year: 'numeric', + }); + } catch (e) { + console.error(`Invalid toLocaleString locale: en-US, date: ${date}`, e); + return ''; + } +}; export const toSelectablePeriod = ( date: Date, label?: string, @@ -60,9 +72,7 @@ export const toSelectablePeriod = ( year, month, dayCount, - label: - label || - date.toLocaleString('en-US', { month: 'long', year: 'numeric' }), + label: label || safeLocaleString(date), }; }; diff --git a/frontend/src/utils/formatDate.ts b/frontend/src/utils/formatDate.ts index a8c073007c..65ac97cd8e 100644 --- a/frontend/src/utils/formatDate.ts +++ b/frontend/src/utils/formatDate.ts @@ -3,7 +3,7 @@ export const formatDateYMDHMS = ( locale?: string, ): string => { try { - return new Date(date).toLocaleString(locale, { + return new Date(date).toLocaleString(locale ?? 'en-US', { day: '2-digit', month: '2-digit', year: 'numeric', @@ -26,7 +26,7 @@ export const formatDateYMDHM = ( timeZone?: string, ): string => { try { - return new Date(date).toLocaleString(locale, { + return new Date(date).toLocaleString(locale ?? 'en-US', { day: '2-digit', month: '2-digit', year: 'numeric', @@ -49,7 +49,7 @@ export const formatDateYMD = ( timeZone?: string, ): string => { try { - return new Date(date).toLocaleString(locale, { + return new Date(date).toLocaleString(locale ?? 'en-US', { day: '2-digit', month: '2-digit', year: 'numeric', @@ -69,7 +69,7 @@ export const formatDateHM = ( locale: string, ): string => { try { - return new Date(date).toLocaleString(locale, { + return new Date(date).toLocaleString(locale ?? 'en-US', { hour: '2-digit', minute: '2-digit', });