1

While i setting value dynamically to react-datepicker throwing an error "RangeError: Invalid time value" enter image description here

This is my datepciker code

<Controller
   control={control}
   className="form-control"
   name="from_date"
   render={({ field }) => (
           <DatePicker
                onChange={(e) => field.onChange(e)}
                selected={field.value}
                dateFormat="dd-MM-yyyy"
                minDate={new Date()}
           />
          )}
  {...register("from_date", { required: true })}/>

This is how i setting value to datepicker

var mydate = moment(date_from_db, 'DD-MM-YYYY').format("DD-MM-YYYY");
setValue('date_field', mydate)

Can anyone explain whats wrong with my code?

Tijo Titus
  • 143
  • 1
  • 8
  • Hi! Why have you `Controller` and `register` with the same `name`? Can you provide a codesanbox with your issue, thank you – Joris Jun 20 '21 at 12:38

1 Answers1

-1

Try doing

<Controller
   control={control}
   className="form-control"
   name="from_date"
   render={({ field }) => (
           <DatePicker
                onChange={(e) => field.onChange(e)}
                selected={field.value}
                dateFormat="dd-MM-yyyy"
                minDate={new Date()}
           />
          )}
  {...register("from_date", { required: true })}/>

var mydate = moment(date_from_db).format("DD-MM-YYYY");
setValue('from_date', mydate)
ZygD
  • 22,092
  • 39
  • 79
  • 102
  • 4
    Please don't post only code as answer, but also provide an explanation what your code does and how it solves the problem of the question. Answers with an explanation are usually more helpful and of better quality, and are more likely to attract upvotes. – atline Sep 11 '21 at 11:57