In my MUI form I'm using DatePicker
to select dates for departure and return of users. But when I used .toLocaleDateString()
it set my dates in mm-dd-yyyy
format. But I wanted to set them as dd-mm-yyyy
format. For that I've passed 'en-GB'
as parameter in .toLocaleDateString()
. But in the DatePicker TextField
it shows a red border Like this and the pre-defined date (from the state) are also gone. Without the parameter it shows This which is the mm-dd-yyyy
format. From the similar previous questions I even tried it through moment
package and set the dt
variable as moment(new Date()).format("DD/MM/YYYY")
but it is still showing red border around the text field. I know this question was asked a lot of times before and I went through all of them and got no solution.
Date picker
const dt = new Date().toLocaleDateString();
console.log(dt);
const [formData, setFormData] = useState({
from: "",
to: "",
depart: dt,
return: dt,
noOfPassenger: 1,
tripType: "Return",
});
{/* App component */}
<div className="dates">
<LocalizationProvider dateAdapter={AdapterDateFns}>
<DatePicker
label="Departure Date"
disablePast
onChange={(e) =>
setFormData({
...formData,
depart: e.toLocaleDateString(),
})
}
value={formData.depart}
renderInput={(params) => <TextField {...params} required />}
/>
</LocalizationProvider>
</div>
TextField
like here: codesandbox.io/s/p268w?file=/demo.js orDesktopDatePicker
like here: codesandbox.io/s/uuonn?file=/demo.js – IndiscretioninputFormat
in the datepicker but it didn't work..later I solved this by addingdate-fns
package and the set the input format in the datepicker – Kimura