Javascript React hook form-提交时,将日期对象转换为数组中的字符串日期
我有以下数据Javascript React hook form-提交时,将日期对象转换为数组中的字符串日期,javascript,reactjs,Javascript,Reactjs,我有以下数据 { status: "Reserved", label: "Note", title: "Login Fragment - Navigation component With Coroutine ", shareWith: "", notification_method: "Every Morning", notificatio
{
status: "Reserved",
label: "Note",
title: "Login Fragment - Navigation component With Coroutine ",
shareWith: "",
notification_method: "Every Morning",
notification_method_specific_time: Sat Jan 09 2021 00:00:00 GMT+0700 (Western Indonesia Time),
noteDetails: Array(2)
0:
completed_date: ""
description: "Test 1"
due_date: Sat Jan 09 2021 20:00:00 GMT+0700 (Western Indonesia Time) {}
__proto__: Object
1:
completed_date: ""
description: "Test 2"
due_date: Sun Jan 10 2021 20:15:00 GMT+0700 (Western Indonesia Time) {}
__proto__: Object
length: 2
__proto__: Array(0)
}
案例是如何从日期格式(对象)转换为日期字符串
const convertDate = (date) => {
return date ? moment(date).format('YYYY-MM-DD HH:mm') : null;
}
const onSubmit = (data) => {
const formData = {
...data,
notification_method_specific_time: convertDate(data.notification_method_specific_time),
}
console.log(formData);
// notification_method_specific_time: "2021-01-09 00:00"
}
但是数组中的date(due\u date)元素呢,请提供帮助。您需要在noteDetails中映射并转换
due\u date
const formData = {
...data,
notification_method_specific_time: convertDate(data.notification_method_specific_time),
noteDetails: data.noteDetails.map(note => ({
...note,
due_date: convertDate(note.due_date)
})
}