React Antd DatePicker with custom format for input and for display

百般思念 提交于 2021-02-11 15:29:55

问题


I am currently using DatePicker Component of antd for displaying date but not able to customize the input format and display format.

Eg: user would type the date as mmddyy (112119) format, datePicker should display date as 2019-11-21.

Please find the sandbox link which I have tried by setting the value to datePicker but its getting overridden by format attribute

https://codesandbox.io/s/wonderful-star-75qjq


回答1:


Inspect the onOpenChange event and change the format prop can do this.

class DateInput extends React.Component {
  state = { isOpen: false };
  render() {
    return (
      <DatePicker
        onChange={onChange}
        format={this.state.isOpen ? "MMDDYYYY" : "YYYY-MM-DD"}
        onOpenChange={status => {
          this.setState({ isOpen: status });
        }}
      />
    );
  }
}

Codesandbox demo here



来源:https://stackoverflow.com/questions/59495987/react-antd-datepicker-with-custom-format-for-input-and-for-display

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!