简体   繁体   English

如何在antd中将今天日期设置为默认日期?

[英]How to set today date as a default date in antd?

I want to set today date as a default selected date in ant design date picker.我想将今天日期设置为蚂蚁设计日期选择器中的默认选定日期。 how can I do that?我怎样才能做到这一点?

<DatePicker
 onChange={this.onChange}
 defaultValue={moment("YYYY-MM-DD")}
 />

I am trying to do that using this line it is not working.我正在尝试使用这条线来做到这一点,但它不起作用。

Just call moment without arguments and separate the format to its property.只需调用不带参数的moment并将格式与其属性分开即可。

const dateFormat = 'YYYY/MM/DD';

ReactDOM.render(
  <div>
    <DatePicker defaultValue={moment()} format={dateFormat} />
  </div>,
  document.getElementById('container'),
);

编辑 Q-59518823-DatePicker

you can set a function on mounted to create today's date您可以在mounted上设置一个函数来创建今天的日期

data() {
  return {
    date: ''
  };
},
methods: {
  getDate() {
    const 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;

    this.date = [year, month, day].join('-');
  },
},
mounted() {
  this.getDate();
}

then you can pass this.date value into the default value like this:然后您可以将 this.date 值传递给默认值,如下所示:

<DatePicker
 onChange={this.onChange}
 defaultValue={moment(date)}
 />

if this script not working : [year, month, day].join('-') you can change to ${year}-${month}-${day}如果此脚本不起作用: [year, month, day].join('-')您可以更改为${year}-${month}-${day}

tell me if this still not work for your problem because from what i'm understand, you are tried to set the default value by today date right?告诉我这是否仍然无法解决您的问题,因为据我所知,您试图在今天日期之前设置默认值,对吗?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM