将数据发布到API之前,将日期格式化为“ YYYY-MM-DD”,Redux-Saga

问题描述 投票:1回答:1

我使用Formik模块处理项目中“ react-hichestan-datetimepicker”模块的表单和DateInput。DateInput的输出为“ 2019-10-25T20:30:00.000Z”。我想以“ 2019-10-25”的格式发送日期值到API。我在Formik的onSubmit属性中更改了格式。

<Formik
  initialValues={{
    date: ""
  }}
  onSubmit={(values, action) => {
    const data = { 
      ...values, 
      date: values.date.split("T")[0]
    };
    dispatch(
      createWorkingShift({ data, history: props.history })
    );
  }}
//...

这里是传奇:

const { data, history } = action.payload;
  try {
    const createWorkingShift = yield call(async () => {
      const workingShift = await httpService.post(
        "http://api.../v1/manager/ws/create",
        data
      );
      if (workingShift.status !== 200) {
        throw new Error("failed");
      }
      return {
        data: workingShift.data.working_shift,
        message: "success"
      };
    }); 
    yield put({ type: CREATE_WORKING_SHIFT_SUCCESS, payload: createWorkingShift });
    //...

有动作:

export const createWorkingShift = payload => {
  return {
    type: CREATE_WORKING_SHIFT_REQUESTED,
    payload
  };
};

日期格式在onSubmit中的数据变量中正确更改,但数据未发送到API

当我删除date: values.date.split("T")[0]数据传送成功时!

我不知道问题出在哪里?

感谢您的帮助。

reactjs redux redux-saga
1个回答
1
投票

我建议您使用MomentJS

const formatedDateString = moment(yourdate).format('YYYY-MM-DD').toString();
© www.soinside.com 2019 - 2024. All rights reserved.