场景:项目里的表单使用到了时分格式的时间选择器,从接口数据 format 到表单数据。#
- 使用 moment 的 format 正常
- 使用 dayjs 的 format 导致转换后时间为 NaN,导致 setFieldsValue 时报错。
阅读官网的文档后发现有一个官方插件是用于拓展 dayjs 的 format 自定义时间格式。#
插件名:CustomParseFormat
import dayjs from 'dayjs'
import customParseFormat from 'dayjs/plugin/customParseFormat'
// 使用
dayjs('12:21', 'HH:mm') // 不使用插件 M2{$H:NaN,$m:NaN,...}
dayjs.extend(customParseFormat)
dayjs('12:21', 'HH:mm') //使用插件 M2{$H:12,$m:21,...}