2

I want to set a default date in my input date field but its not working.

 const [defaultDateFrom, setDefaultDateFrom] = useState<dayjs | null>(null);

 const dd = new Date(currentYear, currentMonth, currentDay, 10);
 const d = dayjs(dd).add(1, 'day');
 setDefaultDateFrom(d);

  <section className="date-container">
    <input type="datetime-local" value={defaultDateFrom} />
  </section>

What I am doing wrong ?

reacter777
  • 67
  • 4
  • From what i see your code must be going into infinite loop, if not can you provide a better snippet as this is a bit unclear from my perspective. Can you be more specific on what is not working means are you getting any error in console or warning or anything which can help debug your issue in better manner. – Milan Patel Mar 07 '23 at 16:53

1 Answers1

2

The datetime-local input requires a string value in a specific format. You are passing in a dayjs object. The solution is to format the object to a datetime string, like this:

const d = dayjs(dd).add(1, "day").format("YYYY-MM-DDThh:mm")

You can check out a working example here.

Also note that in your example, the component re-renders indefinitely.

svrbst
  • 151
  • 3