0

I am using the html input type date on a form in react. Previously when encountering issues with the javascript date object and getting it to display correctly, I used the method of .replace(/-/g, "/") and the day passed in would be displayed correctly.

I am making a new app and trying the same thing and receiving the following error basically copying and pasting lines of code.

The specified value "2021/07/06" does not conform to the required format, "yyyy-MM-dd".

So apparently you are required to do a timezone offset to get the date object to correctly display in react?

Here is what i am trying to do to get a proper date to display in the value of the date picker:

      <input
       type='date'
       name='startDate'
       value={startDisplay != null && startDisplay}
       id=''
       onChange={(e) => {
        setStartDate(e.target.value);
        setStartDisplay(
         Intl.DateTimeFormat({
          year: "numeric",
          month: "numeric",
          day: "numeric",
         })
          .format(new Date(startDate))
          .replace(/-/, "/")
          .replace(/-/, "/")
        );
       }}
      />

I'm wondering if there is a fix that allows the replace method to work, or if someone can help me write a function for setDisplayDate that properly uses timezoneoffset.

or should I just suck it up and add the date picker library?

Mickey Gray
  • 440
  • 2
  • 11
  • `setStartDate` is asynchronous. There is no guarantee `startDate` will be set by the time `sstStartDisplay` is called. Assuming the code `const [startDate, setStartDate] = useState();` or similar was used. – Heretic Monkey Jul 29 '21 at 17:46
  • Also, the `value` of an `input type="date"` must be in a form of ISO 8601. See [Set date in input type date](https://stackoverflow.com/q/12346381/215552) – Heretic Monkey Jul 29 '21 at 17:48
  • I think this is a less than ideal example stacking the functions like that, all the same the point is replacing "-" with "/" to ignore the time component of a date no longer functions in react which is a bummer – Mickey Gray Jul 29 '21 at 17:57

0 Answers0