简体   繁体   中英

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

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" : "") }
 />

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 ie FORMAT

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM