Material UI 日期选择器 日期格式 React

Jus*_*oob 8 datepicker material-ui react-redux

我看不到在 React 应用程序中格式化 Material UI 日期选择器??

当用户选择一个日期时,我希望它的格式为 MM/DD/YYYY。我查了几个答案,但不清楚更改格式的功能应该去哪里???由于某种原因,它没有明确的功能/位置在线>>

日期选择器组件

import React from 'react';
import DatePicker from 'material-ui/DatePicker';

/**
 * `DatePicker` can be implemented as a controlled input,
 * where `value` is handled by state in the parent component.
 */
export default class DateSelect extends React.Component {

  constructor(props) {
    super(props);

    this.state = {
      controlledDate: null,
      openSnackbar: false,
      snackbarText: {
        dateconf: 'Date has been entered!'
      }
    };
  }
  formatDate(date){


  return (date.getMonth() + 1) + "/" + date.getFullYear() + "/" +  date.getDate();
}
  handleChange = (event, date) => {
    console.log('we are in a handle change in date select', event, date)
    this.setState({
      controlledDate: date,
    });
    // this.setState({
        //  openSnackbar: true
        // })
  };

  render() {
    console.log('state and props in date select', this.state, this.props)

    return (
      <DatePicker formatDate={this.formatDate}
        hintText="Date of purchase"
        hintStyle={{color:'whitesmoke'}}
        inputStyle={{ color:'whitesmoke'}}
        value={this.state.controlledDate}
        onChange={this.props.onChange}
      />


    );
  }
}

     ///THE PARENT COMPONENT



   handleDatePicker = (name, date) => {
console.log('handling date change', name, date)
this.setState(prevState => ({
    currentRow: {
        ...prevState.currentRow,
        purchase_date: date
    },
    purchase_date: date,
    actionType: 'date'
}))
this.setState({
    openSnackbar: true
})

   }



                    <DateSelect 
                    hintText="Purchase date"
                    value = {this.state.purchase_date}
                    onChange={this.handleDatePicker}



                    />
Run Code Online (Sandbox Code Playgroud)

Ash*_*shh 5

您需要使用formatDate函数来格式化日期选择器中的日期和时间

formatDate --> 调用此函数来格式化输入字段中显示的日期,并应返回一个字符串。默认情况下,如果未提供区域设置和 DateTimeFormat,则日期对象将格式化为 ISO 8601 YYYY-MM-DD。

<DatePicker
        hintText="Date of purchase"
        hintStyle={{color:'whitesmoke'}}
        inputStyle={{ color:'whitesmoke'}}
        value={this.state.controlledDate}
        onChange={this.props.onChange}
        formatDate={(date) => moment(new Date()).format('MM-DD-YYYY')}
      />
Run Code Online (Sandbox Code Playgroud)

如果您没有传递任何日期格式,则最小和最大日期似乎不起作用。

欲了解更多详细信息,请参阅材料-ui/DatePicker