I have an issue related to rendering date of content within my react application. I've got a date in a format '2021-11-24 20:17:39' from back-end, then I transformed it using the following function:
const getDate = (myDate) =>
new Date(myDate).toLocaleDateString(navigator.language, {
month: 'short',
day: 'numeric',
year: 'numeric',
as a result I got proper date format on every browser on my laptop, like Nov 24, 2021, but the issue was that on IOS I got 'Invalid date' instead of the date I need. Later on, I changed my approach and used the following code:
import { intlFormat } from 'date-fns';
const getDate = (date) => {
const userLocale =
navigator.languages && navigator.languages.length
? navigator.languages[0]
: navigator.language;
return intlFormat(
new Date(date),
{
month: 'short',
day: 'numeric',
year: 'numeric',
},
{
locale: userLocale,
}
);
};
So now the page crushes every time I open it (on IOS) Any ideas would be helpful! p.s. I need exactly this format as a result: Nov 24, 2021. And depending on user browser's location