I am not able to get value in an input field from a custom component.
dateFormat.jsx
import React from "react";
const DateFormat = ({ seconds, nanoseconds, format }) => {
const dates = new Date(seconds * 1000 + nanoseconds / 1000000);
const day = new Date(dates).toLocaleString("default", { day: "2-digit" });
const month = new Date(dates).toLocaleString("default", { month: "2-digit" });
const year = new Date(dates).toLocaleString("default", { year: "numeric" });
if (format === "dd/mm/yyyy") {
return <>{day + "/" + month + "/" + year}</>;
}
return <>{year + "-" + month + "-" + day}</>;
};
export default DateFormat;
When I call this DateFormat in another page like the example below it works fine
<DateFormat
nanoseconds={dt.dob.nanoseconds}
seconds={dt.dob.seconds}
format={"text"}
/>
The output is like below
12/02/2001
However when I want to get the value in a text field or DatePicker it does not work fine
<input
type="date"
className="form-control"
id="input-datetime-local"
value={
<DateFormat
nanoseconds={dt.dob.nanoseconds}
seconds={dt.dob.seconds}
format={"yyyy/mm/dd"}
/>
}
onChange={(e) => {
e.preventDefault();
}}
name="dob"
/>
I am expecting an output as below
<input
type="date"
className="form-control"
id="input-datetime-local"
value="2001/02/12"
onChange={(e) => {
e.preventDefault();
}}
name="dob"
/>
DateFormata utility function instead of a component that returns a stringformatDate()DateFormatcomponent.