I have a JavaScript string date:
js code:
const lastDayDate = new Date(selectedDate.getFullYear(), selectedDate.getMonth() + 1, 0);
const options = { year: 'numeric', month: '2-digit', day: '2-digit' };
const formattedDate = lastDayDate.toLocaleDateString('se-SE', options);
The output of console.log(formattedDate) is something like:
05/31/2023
My question is how to convert it to :
2023-05-31
Any friend can help ?
3
Answers
Try this?
one way:
const formattedDate = lastDayDate.toJSON().slice(0, 10);
Be careful,
lastDayDate.toISOString().split('T')[0]
will return UTC Date instead of Local Date. So the correct way to handle this is withformatDate
function which gets a local date as year, month, and day.