how can i send formated date to api with react-datepicker

Viewed 1286

I'm trying to send a date to my API with react-datepiker with formik to handle the date being nested, and even though I set the dateFormat to yyyy/MM/dd I'm still getting "2020-06-19T04:00:00.000Z" as the value and it is what's being pushed to my API only to get an error to set the date format to yyyy/MM/dd

Any ideas as to what this could be. Is this a bug? I am using react 16.5 and Django with drf for my backend the model uses DateField() and expects yyyy/MM/dd. Is there a workaround that anyone can think of?

Thank you in advance!

<DatePicker
  dateFormat="yyyy/MM/dd"
  selected={values.config[index].date}
  name={`config[${index}]['date']`}
  value={getIn(values, `config[${index}]['date']`) || ""
   }
  onChange={(e) => setFieldValue(`config[${index}] 
  ['date']`, e)}
  className={"form-control" + (errors.date && 
   touched.date? " is-invalid" : "") }
 />
1 Answers

You can pass as an attribute in DatePicker component

    const FORMAT = "yyyy/MM/dd"

    <DatePicker
       dateFormat={FORMAT}
       onChange={event => setFieldValue(FORMAT))} // something like
     />

Also need to pass in onChange event formated date i.e. FORMAT

Related