Recently, because I used the date picker (DatePicker) in Ant Design Pro in my writing project, the data format it gave me was Dayjs, and there was no problem in transferring it to the interface, but the problem came when it was echoed, and the interface returned such as : 2023-04-27 However, it does not recognize this component, and it will report an error. It took a long time to find out that the required format is different.
Dayjs official website: Node.js Day.js
If you want to expand it, you can take a look. If you don’t want to, install it directly:
npm install dayjs
Here comes the most important thing: import
import dayjs from 'dayjs';
import customParseFormat from 'dayjsugin/customParseFormat';
Declaration conversion format:
const dateFormat = 'YYYY/MM/DD';
If the interface returns other formats, you can refer to:
Then it is displayed in the code:
//这里我的编辑和添加是一体的所以加一个三元,为1代表添加
{datatime === 1 ? (
<DatePicker
showTime
//birth是接口返回的数据(看清楚了不是births)
onChange={births}
onOk={onOkss}
defaultValue={null}
format={dateFormat}
/>
) : (
<DatePicker
showTime
onChange={births}
onOk={onOkss}
defaultValue={dayjs(birth, dateFormat)}
format={dateFormat}
/>
)}
Effect: