format string 05.11.2022 into date 11/5/2022 in the moste concise way in javascript

You can easily achieve this by splitting the string into an array based on the "." separator, and then reformatting it into a date string using template literals.

index.tsx
const dateString = "05.11.2022";
const [day, month, year] = dateString.split(".");

const formattedDate = `${month}/${day}/${year}`;

console.log(formattedDate); // Output: "11/5/2022"
185 chars
7 lines

In this code, we first declare the input dateString as a string with the value "05.11.2022". We then split the string into an array called [day, month, year] using split(".") method.

Next, we reformat the date with template literals and store the value in formattedDate variable.

Finally, we log formattedDate to the console, which should output the string "11/5/2022".

gistlibby LogSnag