【问题标题】:Save Date to base | format date in Date Picker of Ant Design将日期保存到基础 |在 Ant Design 的 Date Picker 中格式化日期
【发布时间】:2018-06-08 12:11:30
【问题描述】:

我想将日期保存到我的基地,格式如下:“1983 年 9 月 29 日”,但我得到:“Thu Jun 28 2018 14:44:33 GMT+0300”。我做错了什么?

const dateFormat = "D MMMM YYYY";

class Create extends Component {
  constructor(props) {
    super(props);
    this.state = {
      date_created: moment()
    };

    this.handleDateCreatedChange = this.handleDateCreatedChange.bind(this);
  }

    fetch("http://localhost:8000/invoices", {
      method: "post",
      body: JSON.stringify({
        date_created: this.state.date_created
      })
    })
  }

  handleDateCreatedChange(event) {
    console.log(event);
    this.setState({ date_created: event.toString() });
  }

  render() {
    return (
       <DatePicker style={{ width: 630 }}
                   value={moment(this.state.date_created, dateFormat)}
                   format={dateFormat}
                   onChange={this.handleDateCreatedChange}
       />

.....等等

【问题讨论】:

  • 请确保您在此处添加的代码以可读的方式格式化,例如均匀压痕。另外,“基地”是什么意思?数据库?
  • 我的意思是 json-server
  • @OkK 你检查我的答案了吗?

标签: javascript reactjs date momentjs


【解决方案1】:

您必须将值格式化为:

fetch("http://localhost:8000/invoices", 
    {
        method: "post",
        body: JSON.stringify(
            {date_created: this.state.date_created.format('D MMMM YYYY')} // format date here
        )
    }
)

handleDateCreatedChange(event) {
    console.log(event);
    // No need to format
}

render() {
    ...
    <DatePicker 
        ...
        value={this.state.date_created}
        ...
    />
    ...
}

【讨论】:

  • 但是我得到一个错误:“错误:DatePicker 或 MonthPicker 的 value/defaultValue 必须是 antd@2.0 之后的时刻对象,请参阅:u.ant.design/date-picker-value
  • 非常感谢!正如你所说,我在 fetch 中添加了 .format('D MMMM YYYY')
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2023-01-22
  • 2018-04-27
  • 2021-01-23
  • 2023-01-03
  • 1970-01-01
  • 1970-01-01
  • 2019-07-16
相关资源
最近更新 更多