1
0
mirror of https://github.com/Unleash/unleash.git synced 2025-01-20 00:08:02 +01:00

Add try catch to toLocaleString

This commit is contained in:
Gastón Fournier 2024-09-02 16:25:57 +02:00
parent 702dbffd3d
commit 1208a9ac02
No known key found for this signature in database
GPG Key ID: AF45428626E17A8E
2 changed files with 59 additions and 27 deletions

View File

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

View File

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