diff --git a/frontend/src/hooks/useTrafficData.ts b/frontend/src/hooks/useTrafficData.ts index 00c6e184d9..bbd7b74ef6 100644 --- a/frontend/src/hooks/useTrafficData.ts +++ b/frontend/src/hooks/useTrafficData.ts @@ -62,7 +62,7 @@ export const toSelectablePeriod = ( dayCount, label: label || - "date.toLocaleString('en-US', { month: 'long', year: 'numeric' })", + date.toLocaleString('en-US', { month: 'long', year: 'numeric' }), }; }; diff --git a/frontend/src/utils/formatDate.ts b/frontend/src/utils/formatDate.ts index 86729a5d12..a8c073007c 100644 --- a/frontend/src/utils/formatDate.ts +++ b/frontend/src/utils/formatDate.ts @@ -2,14 +2,22 @@ export const formatDateYMDHMS = ( date: number | string | Date, locale?: string, ): string => { - return new Date(date).toLocaleString(locale, { - day: '2-digit', - month: '2-digit', - year: 'numeric', - hour: '2-digit', - minute: '2-digit', - second: '2-digit', - }); + try { + return new Date(date).toLocaleString(locale, { + day: '2-digit', + month: '2-digit', + year: 'numeric', + hour: '2-digit', + minute: '2-digit', + second: '2-digit', + }); + } catch (e) { + console.error( + `Invalid toLocaleString locale: ${locale}, date: ${date}`, + e, + ); + return ''; + } }; export const formatDateYMDHM = ( @@ -17,14 +25,22 @@ export const formatDateYMDHM = ( locale: string, timeZone?: string, ): string => { - return new Date(date).toLocaleString(locale, { - day: '2-digit', - month: '2-digit', - year: 'numeric', - hour: '2-digit', - minute: '2-digit', - timeZone, - }); + try { + return new Date(date).toLocaleString(locale, { + day: '2-digit', + month: '2-digit', + year: 'numeric', + hour: '2-digit', + minute: '2-digit', + timeZone, + }); + } catch (e) { + console.error( + `Invalid toLocaleString locale: ${locale}, date: ${date}`, + e, + ); + return ''; + } }; export const formatDateYMD = ( @@ -32,20 +48,36 @@ export const formatDateYMD = ( locale: string, timeZone?: string, ): string => { - return new Date(date).toLocaleString(locale, { - day: '2-digit', - month: '2-digit', - year: 'numeric', - timeZone, - }); + try { + return new Date(date).toLocaleString(locale, { + day: '2-digit', + month: '2-digit', + year: 'numeric', + timeZone, + }); + } catch (e) { + console.error( + `Invalid toLocaleString locale: ${locale}, date: ${date}`, + e, + ); + return ''; + } }; export const formatDateHM = ( date: number | string | Date, locale: string, ): string => { - return new Date(date).toLocaleString(locale, { - hour: '2-digit', - minute: '2-digit', - }); + try { + return new Date(date).toLocaleString(locale, { + hour: '2-digit', + minute: '2-digit', + }); + } catch (e) { + console.error( + `Invalid toLocaleString locale: ${locale}, date: ${date}`, + e, + ); + return ''; + } };