murari_sabavath
murari_sabavath

Reputation: 315

prepopulate date input field with date react

I have a update form where I update the data that I get from backend. I have a date input field and I have a date which is in the 'yyyy-mm-dd' format. I want to show the date field with the date which I'm getting from backend as a default date on initial render. How do I acheive this in react js.

Upvotes: 2

Views: 3401

Answers (1)

kyle4real
kyle4real

Reputation: 86

You could use moment library to convert your date to a moment obj and then use the format method to format it properly for the input field.

const Form = ({ yourDate }) => {
    const [date, setDate] = useState(moment(yourDate).format("YYYY-MM-DD"));

    return <input type="time" value={date} />
}

Upvotes: 4

Related Questions