I'd like to have my date in a dd-mm-yyyy format, so I wrote the below in order to reassemble the date I get from my date picker.
Instead of getting 17-05-2021
, I'm getting 1,7-0,5-2,0,2,1
What can I do to avoid those commas?
I'm open to other suggestions on how to get this date in a more efficient way than what I wrote
const minDate = new Date().toISOString().slice(0, 10) // outputs 2021-05-17
const [text, setText] = useState('');
const [urgent, setUrgent] = useState(false);
const [date, setDate] = useState(minDate);
const formatDate = () => {
let tempDate = [...date];
let day = tempDate.slice(8);
let month = tempDate.slice(5, 7);
let year = tempDate.slice(0, 4);
let newDate = `${day}-${month}-${year}`;
return newDate;
};
const handleSubmit = () => {
let fixedDate = formatDate(date);
console.log(newDate) // outputs 1,7-0,5-2,0,2,1
if (text.length > 5) {
props.addTask(text, urgent, fixedDate);
setText('');
setUrgent(false);
setDate(minDate);
} else alert('Task name too short!');
};
The date picker
<input
type="date"
min={minDate}
value={date}
onChange={(event) => setDate(event.target.value)}
/>