Change my change, do not add 1 to the day of the month
Edit in josh answer to fix getDay for getDate, since getDay returns the day of the week and getDate returns the day of the month.
<input type="datetime-local" value={this.state.datetime} onChange={e => this.handleChange('datetime', e)} />
Since this is a controlled component, you must set the status value to read. I set the current date and time in this state ...
state = { datetime: '${new Date().getFullYear()}-${'${new Date().getMonth() + 1}'.padStart(2, 0)}-${'${new Date().getDate()}'.padStart( 2, 0 )}T${'${new Date().getHours()}'.padStart( 2, 0 )}:${'${new Date().getMinutes()}'.padStart(2, 0)}' };
and my handleChange can be reused for other text inputs, like so:
handleChange = (field, e) => { this.setState({ [field]: e.target.value }); };
Randy brookins
source share