I am currently working on a project using NextJs. One of the issues I am facing involves creating activities for users on a page using useState and fetch POST. Although most things seem to be functioning correctly, I am experiencing difficulties with handling dates.
Whenever I attempt to post, an error message pops up saying: "The JSON value could not be converted to System.DateTime". This indicates that I need to format the date in ISO standard.
The challenge lies in converting a date set using useState in a Textfield from MUI into ISO format. Despite trying various solutions, none have yielded successful results.
To provide more context, below are snippets of my code:
import {Textfield} from "@mui/material";
function CreateActivity {
var today = new Date();
var dd = String(today.getDate()).padStart(2, "0");
var mm = String(today.getMonth() + 1).padStart(2, "0");
var yyyy = today.getFullYear();
today = yyyy + "-" + mm + "-" + dd;
const [activityDate, setActivityDate] = useState(today + "T10:30");
return (
<div className={styles.textBox}>
<div>Date, time</div>
<TextField
variant="standard"
id="datetime-local"
type="datetime-local"
defaultValue={today + "T10:30"}
InputLabelProps={{
shrink: true,
}}
onChange={(event) => setActivityDate(event.target.value)}
/>
</div>
)
}