admin管理员组文章数量:1289866
I am using material-ui datepicker. What i am trying to do is get the value selected by the user and set it to state however i am struggling to figure this out so any help would be much appreciated.
So here is what i am doing at the moment:
My datepicker ponent looks like this:
<DatePicker
hintText="Select the date"
formatDate={(date) => moment(date).format('DD-MM-YYYY')}
onChange={this.handleChange1.bind(this)} />
the handleChange1 function:
handleChange1(e, date){
this.setState({
appointmentDate: date.value
})
console.log(e, date);
console.log(this.state.appointmentDate;
}
The Constructor:
constructor(props){
super(props);
this.state = {
appointmentDate: '',
appointmentTime: ''
};
All of the above give me the following in the console....
null Fri Oct 20 2017 16:50:33 GMT+0100 (BST)
AND
_blank log_
However, in the textfield once the user selects the date i can see the date being rendered correctly like 20-10-2017
The date being displayed to the user is what i want to store to state. Can someone explain how this can be achieved?
Thanks
I am using material-ui datepicker. What i am trying to do is get the value selected by the user and set it to state however i am struggling to figure this out so any help would be much appreciated.
So here is what i am doing at the moment:
My datepicker ponent looks like this:
<DatePicker
hintText="Select the date"
formatDate={(date) => moment(date).format('DD-MM-YYYY')}
onChange={this.handleChange1.bind(this)} />
the handleChange1 function:
handleChange1(e, date){
this.setState({
appointmentDate: date.value
})
console.log(e, date);
console.log(this.state.appointmentDate;
}
The Constructor:
constructor(props){
super(props);
this.state = {
appointmentDate: '',
appointmentTime: ''
};
All of the above give me the following in the console....
null Fri Oct 20 2017 16:50:33 GMT+0100 (BST)
AND
_blank log_
However, in the textfield once the user selects the date i can see the date being rendered correctly like 20-10-2017
The date being displayed to the user is what i want to store to state. Can someone explain how this can be achieved?
Thanks
Share Improve this question asked Oct 20, 2017 at 16:02 Jay240692Jay240692 1761 gold badge5 silver badges17 bronze badges2 Answers
Reset to default 3you are getting the date you are just missing the formatting when you have this information "Fri Oct 20 2017 16:50:33 GMT+0100 (BST)" do something like this
var date = new Date("Fri Oct 20 2017 16:50:33 GMT+0100 (BST)")
var finaldate = date.getDate() + '-' + (date.getMonth() + 1) + '-' + date.getFullYear()
at the end you will have finaldate = "20-10-2017"
hope this helps.
Thank you
You need to convert the Date-Picker into a controlled input. The object with which the Date-Picker can bind is a Date object. So your initial state should look like
this.state={appointmentDate:null}
Then you can bind your date with the following control.
<DatePicker
hintText="Select the date"
formatDate={(date) => moment(date).format('DD-MM-YYYY')}
onChange={this.handleChange1.bind(this)}
value={this.state.appointmentDate}/>
with the handlechange function being
handleChange1(e, date){
this.setState({
appointmentDate: date
});
}
If you want to store the formatted string of the date as a separate entry in your state, you can do so using the handleChange1 function
handleChange1(e, date){
this.setState({
appointmentDate: date,
appointmentDateString: moment(date).format('DD-MM-YYYY')
});
}
本文标签: javascriptHow to get MaterialUI Date Picker valueStack Overflow
版权声明:本文标题:javascript - How to get Material-UI Date Picker value - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741402237a2376735.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论