aure87
aure87

Reputation: 21

How to set a default value for input type date

I would like to set the default date to the last day of the month when the date picker opens on the browser. How can I achieve this?

Upvotes: 1

Views: 223

Answers (2)

rszf
rszf

Reputation: 201

I suggest you use the moment library. It's much easier to do any date related thing with that: https://momentjs.com/

<DatePicker
  selected={moment().endOf('month').format('YYYY-MM-DD')}
/>

Upvotes: 0

MalwareMoon
MalwareMoon

Reputation: 886

This should do:

const now = new Date();
const lastDayOfMonth = new Date(now.getFullYear(), now.getMonth() + 1, 0);
const defaultDate = lastDayOfMonth.toISOString().substring(0, 10);

return (
  <div className="App">
    <input
      type="date"
      defaultValue={defaultDate}
    />
  </div>
);

Upvotes: 1

Related Questions