25

I have a react app. There is a checkbox which disables the datepicker. But I can't select any date when I'm using checkbox to disable it. If I remove checkbox and its function there is no error. Currently, I'm getting:

date.clone is not a function

error.

const dateFormat = "YYYY-MM-DD";
const today = moment();

const [date, setDate] = useState(today);
const [disabled, setdisabled] = useState(false);

  

const onCheckboxHandle = (e) => {
    if (e.target.checked) {
      setwarntill(moment("2090-10-10"));
      setdisabled(true);
    } else {
      setwarntill(today);
      setdisabled(false);
    }
  };
<Checkbox onChange={onCheckboxHandle}>Süresiz</Checkbox>
        <Form.Item name={["user", "timetill"]} label="Uyarı Bitiş Tarihi">
          <ConfigProvider locale={locale}>
            <DatePicker
              defaultValue={moment()}
              format={dateFormat}
              onChange={(date,dateString) => setwarntill(dateString)}
              value={warntill}
              disabled={disabled}
            />
          </ConfigProvider>
        </Form.Item>
double-beep
  • 5,031
  • 17
  • 33
  • 41
Gunsela
  • 360
  • 1
  • 7
  • 18

11 Answers11

31

parsing the date with the help of moment works for me moment(myDate)

Sjonchhe
  • 790
  • 8
  • 16
9

Add valuePropName to <Form.Item>

e.g.

<Form.Item valuePropName={'date'}>
    <DatePicker/>
</Form.Item>
sun_jara
  • 1,736
  • 12
  • 20
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Sep 27 '22 at 13:06
  • 2
    This worked for me. The critical part is that the error is thrown for DatePickers inside of a . Thanks for the answer! – Michael K Nov 05 '22 at 14:05
  • 1
    Feels like magic, but worked for me as well – ellockie Jan 09 '23 at 22:29
  • This should be selected as the correct answer... – Matsu Jan 12 '23 at 03:07
  • Works like magic! – sun_jara Mar 13 '23 at 08:31
6

Put DatePicker component outside <Form.Item > <Form.Item /> and check it will work fine.

         <DatePicker
            format={"YYYY-MM-DD"}
            onChange={(date, dateString) =>
              this.handleDatepickerChange(date, dateString)
            }
            placeholder="Start Date"
            value={
              this.state.startDate !== ""
                ? moment(this.state.startDate)
                : ""
            }
          />
2

I have realized that when using the inside <Form.Item > <Form.Item /> it will drop 'date.clone is not a function' error. So you can put the outside of <Form.Item > <Form.Item /> it should work.

Your code should look like this:

const dateFormat = "YYYY-MM-DD";
const today = moment();

const [date, setDate] = useState(today);
const [disabled, setdisabled] = useState(false);



const onCheckboxHandle = (e) => {
    if (e.target.checked) {
      setwarntill(moment("2090-10-10"));
      setdisabled(true);
    } else {
      setwarntill(today);
      setdisabled(false);
    }
  };

<Checkbox onChange={onCheckboxHandle}>Süresiz</Checkbox>
       <ConfigProvider locale={locale}>
            <DatePicker
              defaultValue={moment()}
              format={dateFormat}
              onChange={(date,dateString) => setwarntill(dateString)}
              value={warntill}
              disabled={disabled}
            />
          </ConfigProvider>
Mistico
  • 231
  • 3
  • 4
  • I just found that you can use DatePicker inside `` by removing `initialValue` or `defaultValue`. – AUPMA Dec 18 '20 at 13:15
1

I got the same issue, it's nothing to do with Form.Item

I have them in Form.Item

I solved this by:

  1. initialise the form value when the page load

  2. when you initialise, remember, antD default locale is US, therefore, you need to convert your moment or string to moment with MM/DD/YYYY

then this solve my issue

KeepLearning
  • 327
  • 6
  • 13
1

Try this, this worked for me

const today = moment(new Date().getDate(),'DD/MM/YYYY')
Vimal Saifudin
  • 1,815
  • 1
  • 21
  • 28
1

I faced with the similar issue (date.clone is not a function) when tryed to use saved as a string type data for DatePicker in initialValues of <Form>. Solved it by passing to initial values not string, but the moment object for DatePicker:

  const dateFormat = 'YYYY-MM-DD';
  const selectedStartDate = moment('2000-11-11', dateFormat);
  const selectedEndDate = moment('2000-12-11', dateFormat);

  const initValues = {
    startDate: selectedStartDate,
    endDate: selectedEndDate,
  };

  <Form form={form}
        name="basic"
        initialValues={initValues}>
    
    <Form.Item label={'Start'} name={'startDate'}>
      <DatePicker value={selectedStartDate}/>
    </Form.Item>

    <Form.Item label={'End'} name={'endDate'}>
      <DatePicker value={selectedEndDate}/>
    </Form.Item>

  </Form>
Teriel
  • 11
  • 4
0

Give a ternanry condition of isNull then it works.... Something like

date_added: _.isNull(date_added) ? null : moment(date_added);
cigien
  • 57,834
  • 11
  • 73
  • 112
0

the value need a moment.js instance. you should change the onChange function to this onChange={(date,dateString) => setwarntill(date)}. and when post to server you should use moment format function to get what server needs format.

PeakFish
  • 45
  • 6
0

Try this, add getValueProps to <Form.Item>

 <Form.Item
  getValueProps={(value) => {
    return { value: dayjs(value) };
  }}
>
  <DatePicker/>
</Form.Item>
叶ciel
  • 1
  • 1
  • You may want to edit your answer to explain why this resolves the asker's question, further guidance can be found in the [Help Center](https://stackoverflow.com/help/how-to-answer) – Harrison Aug 14 '23 at 09:20
-2

This Will Fix your issue.

Use defaultValue={moment(moment(), dateFormat)}
STA
  • 30,729
  • 8
  • 45
  • 59