做项目过程中,需要用到自定义的format,比如MM.DD.YYYY之类的, 用antd 的DatePicker发现直接给format属性赋值是无效的,会变成默认的YYYY-MM-DD HH:mm格式。
查看官方api,发现format属性在string模式下,只支持最基本的三种,所以自定义format不能直接给format字符串。
解决:
getCustomDate=(value)=>{
const localeCode = getNeoContext('localeCode') || 'CN';
const format = timeFormatByCountryCode(localeCode) || "HH:mm";
const dateValue = moment(value).format(format);
return dateValue;
}
<DatePicker
value={this.state.valueDate}
mode="date"
onChange={this.onChange}
format={(value: Date) =>{
return this.getCustomDate(value);
}}>
</DatePicker>
其中getCustomDate返回相应的format格式就可以了,比如我这里直接返回的就是MM.DD.YYYY,就可以获取到这个格式的时间了。