Ant Design中DatePicker设置mode="year"及利用getFieldDecorat双向绑定form表单的年份

401 阅读1分钟

1.第一步:由于需要一个年份的日期,所以参考https://juejin.cn/post/6844903859987415047

3.但是这样实现之后会在页面爆出警告


import moment from 'moment';
moment.suppressDeprecationWarnings = true;//避免不规范的时间集进行format发出警告

4.在加上上面的代码就不会爆出警告,如果在日期中默认选中当前的默认年份的时候,可以在双向绑定中初始值给定当前年份---获取当前年份(new Date().toLocaleDateString())

initialValue: params[key] ? moment(params[key]) : moment(new Date().toLocaleDateString()),

4.1 由于在DatePicker中已经formate,所以在moment中不用formate,如果单独用可以用moment(new Date().toLocalDateString(),"YYYY")