Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Material UI (dateTime picker) : Invalid Date Format message after submission

I am using Material Ui DateTime picker in a form. After submitting the form, I get the following error?:

Invalid Date Format

Image

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

I am using JSON Server in my react app for saving data.

This is the output Element for the DateTime picker on DOM.

<input aria-invalid="false" readonly="" type="text" class="MuiInputBase-input MuiInput-input" value="March 16th 08:50 a.m.">

This is data on db.json.

{
  "text": "study",
  "day": "2022-03-16T05:20:00.000Z",
  "reminder": true,
  "id": 1
}

This is my code for Add date.

import { useState } from "react";
import DateFnsUtils from "@date-io/date-fns";
import { MuiPickersUtilsProvider } from "@material-ui/pickers";
import "date-fns";
import { DateTimePicker } from "@material-ui/pickers";

const AddTask = ({ onAdd }) => {

const [day, setDay] = useState(new Date());

const onSubmit = (e) => {
  e.preventDefault();

  onAdd({ day });
  setDay("");
};
return (
<form className="add-form" onSubmit={onSubmit}>
 <div className="form-control">
    <label>Day & time</label>
    <MuiPickersUtilsProvider utils={DateFnsUtils}>
      <DateTimePicker
        disableToolbar
        variant="inline"
        value={day}
        onChange={(day) => {
          setDay(day);
        }}
        autoOk
      />
    </MuiPickersUtilsProvider>
  </div>
</form>
);
};

I would appreciate it if you help me. Thanks

>Solution :

It will probably not break if you remove the setDay(""); line from onSubmit. Why do you need it after all?

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading