3

I have a Signup form that takes a date as input.

The code of my date input is:

birthDate: new Date(),

this.handleSubmit = this.handleSubmit.bind(this);
this.handledChange = this.handledChange.bind(this);


handledChange(date) {
  this.setState({
    birthDate: date
  });
}

<ControlLabel>Date de naissance</ControlLabel>
  <FormGroup controlId="birthDate" bsSize="large">
    <FormControl
      autoFocus
      type="date"
      value={this.state.birthDate}
      onChange={this.handledChange}
    />
  </FormGroup>

When I submit it, the input date is undefined on the console.

I want it in the format : YYYY-MM-DD

How can I fix that please?

3 Answers 3

4

You won't get selected date directly it should be through event target

Change

handledChange(date) {
    this.setState({
      birthDate: date
    });
  }

To

handledChange(event) {
    this.setState({
      birthDate: event.target.value
    });
  }
Sign up to request clarification or add additional context in comments.

1 Comment

It is still the same issue, it is undefined
1

You can also extract the input value of your form using its Id, after submission.
You can try using like this in your submit method:

let selectedDate = document.getElementById('birthDate')

BirthDate here refers to controlId of FormGroup

2 Comments

it is the same issue
Just a small question that I can think of from the top of my head. Did you make sure whether your submit handler is being called upon submission?
1

This works like a gem. please try it out if you are still facing the issue

             <input
                type="date"
                name="expiration date"
                value={this.state.expiration_date}
                onChange={event => this.setState({expiration_date: event.target.value})} 
              />
onChange = (key, value) => {
    this.setState({
      [key]: value
    })
  }

2 Comments

What are the advantages of this over the accepted answer? When adding answers to older questions with accepted answers it is really important to point out what new aspect of the question your answer addresses. Code only answers can almost always be improved by the addition of some explanation of how and why they work.
@JasonAller what accepted answer? There's no accepted answer at all and OP says those answers are not working for him. In fact, Aravind's way is the correct way.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.