I need to convert a date from "DD-MM-YYYY" format to "D MonthName YYYY" format, and viceversa - where "MonthName" is in French.
The first task is quite easy:
var str = "02-04-2023";
[day, month, year] = str.match(/\d+/g);
var dt = new Date(year, month - 1, day).toLocaleDateString("fr-FR", {
day: "numeric",
month: "long",
year: "numeric"
})
console.log(dt);
But how to do the opposite (in Vanilla JavaScript)? I.e.: how to get "02-04-2023" from "2 avril 2023"?
I don't want to use any array/object of months.
Any suggestions would be much appreciated.
What I have tried:
var str = "02-04-2023";
[day, month, year] = str.match(/\d+/g);
var dt = new Date(year, month - 1, day).toLocaleDateString("fr-FR", {
day: "numeric",
month: "long",
year: "numeric"
})
console.log(dt);
(But maybe there's a better solution for this)