最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - How to format get and format Date in DatePicker in Material UI React - Stack Overflow

programmeradmin0浏览0评论

I have a datepicker and want to get only date in format YYYY-MM-DD. The default datetime format is:

Tue Feb 06 2018 00:00:00 GMT+0200 (FLE Standard Time)

How can to format this datepicker ? Also how can I change the color of the field to another color, ie the field on which the date is written.

handleChangeDate = (event, date) => {
this.setState({
  controllerDate: date
});
 };

onSubmitHandler(e){
    e.preventDefault();
    console.log(this.state.value)
    console.log(this.state.controllerDate)
  }

 <div className="col-md-3">           
              <DatePicker hintText="Изберете дата" onChange={ this.handleChangeDate } />
 </div>

I have a datepicker and want to get only date in format YYYY-MM-DD. The default datetime format is:

Tue Feb 06 2018 00:00:00 GMT+0200 (FLE Standard Time)

How can to format this datepicker ? Also how can I change the color of the field to another color, ie the field on which the date is written.

handleChangeDate = (event, date) => {
this.setState({
  controllerDate: date
});
 };

onSubmitHandler(e){
    e.preventDefault();
    console.log(this.state.value)
    console.log(this.state.controllerDate)
  }

 <div className="col-md-3">           
              <DatePicker hintText="Изберете дата" onChange={ this.handleChangeDate } />
 </div>
Share Improve this question edited Feb 27, 2018 at 15:31 WebDevBooster 15k9 gold badges68 silver badges70 bronze badges asked Feb 27, 2018 at 14:54 Zlatka PijevaZlatka Pijeva 711 gold badge5 silver badges12 bronze badges 1
  • This is not a Bootstrap issue. – WebDevBooster Commented Feb 27, 2018 at 15:31
Add a ment  | 

2 Answers 2

Reset to default 2

Here is the solution for getting the date in YYYY-MM-DD format

class DatePickerExampleSimple extends React.Component {
    constructor(props) {
        super(props);
        this.state = {}
        this.handleChangeDate = this.handleChangeDate.bind(this);
        this.formatDate = this.formatDate.bind(this);
    }

    formatDate(date) {
        let d = new Date(date),
            month = '' + (d.getMonth() + 1),
            day = '' + d.getDate(),
            year = d.getFullYear();

        if (month.length < 2) month = '0' + month;
        if (day.length < 2) day = '0' + day;

        return [year, month, day].join('-');
    }

    handleChangeDate = (event, date) => {
        let formattedDate = this.formatDate(date);
        console.log(formattedDate);
        this.setState({
            controllerDate: formattedDate
        });
    };

  render(){
      return(<div>
          <DatePicker hintText="Изберете дата" onChange={ this.handleChangeDate } />
          <div>New selected value is {this.state.controllerDate}</div>
      </div>)
  }
};
export default DatePickerExampleSimple;
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react.min.js"></script>
<script src="https://cdnjs.cloudflare./ajax/libs/react/15.1.0/react-dom.min.js"></script>

use moment to get your desire format... using you can get any format you want...

handleChangeDate = (event, date) => {
this.setState({
  controllerDate: moment(date).format('YYYY-MM-DD')
});
 };

onSubmitHandler(e){
    e.preventDefault();
    console.log(this.state.value)
    console.log(this.state.controllerDate)
  }

 <div className="col-md-3">           
              <DatePicker hintText="Изберете дата" onChange={ this.handleChangeDate } />
 </div>
发布评论

评论列表(0)

  1. 暂无评论