1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-06 00:07:44 +01:00

Add try catches everywhere and defaults for locales

This commit is contained in:
Gastón Fournier 2024-09-02 16:37:11 +02:00
parent 1208a9ac02
commit f62dbe6917
No known key found for this signature in database
GPG Key ID: AF45428626E17A8E
2 changed files with 17 additions and 7 deletions

View File

@ -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),
};
};

View File

@ -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',
});