ProForm里包着用到了ProFormDatePicker的“开始时间”,还有其他参数。
<ProForm
formRef={formRef}
>
<ProFormText
name="name"
{...formItemLayout}
width="md"
label="项目名称"
/>
<ProFormDatePicker
name="startDate"
{...formItemLayout}
width="md"
label="开始时间"
/>
<ProFormTextArea
name="description"
{...formItemLayout}
label="项目描述"
width="md"
/>
</ProForm>
将startDate从ProForm的数据中删除。
const { startDate, ...otherFields } = formRef?.current?.getFieldsValue();
const fieldsValue = formRef?.current?.getFieldsValue();
delete fieldsValue.startDate;
我们可以先打印一下formRef?.current?.getFieldsValue(),以确定startDate在表单里的值。然后用如下方法,就将“Sat Apr 29 2023 09:53:44 GMT+0800 (中国标准时间)”的格式转为了“2023-4-29”格式。
let startYear = formRef?.current?.getFieldsValue().startDate._d.getFullYear();
let startMonth =formRef?.current?.getFieldsValue().startDate._d.getMonth() + 1;
let startDay = formRef?.current?.getFieldsValue().startDate._d.getDate();
let startDateResult = `${startYear}-${startMonth}-${startDay}`;
console.log(startDateResult)
//2023-4-29
然后给后端传值的地方除了要传fieldsValue,还要单独传startDate: startDateResult。
其实,原本格式为“2023-04-29T09:53:44.995Z”,只是表单里打印出的是“Sat Apr 29 2023 09:53:44 GMT+0800 (中国标准时间)”,就将计就计进行转换。