0

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

  • '2021-11-24 20:17:39' is not a format supported by ECMA-262 so parsing is implementation dependent. Replacing the central space with the letter "T" ('2021-11-24T20:17:39') converts it to a valid format that will be parsed as local. – RobG Jun 12 '22 at 07:02
  • Passing `navigator.languages[0]` is guaranteed to **not** always produce exactly MMM DD, YYYY as different languages produce different formats. See [*How do I format a date in JavaScript?*](https://stackoverflow.com/questions/3552461/how-do-i-format-a-date-in-javascript) – RobG Jun 12 '22 at 07:04

0 Answers0