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

You can achieve this by using the Date() constructor in JavaScript to parse the string and then using string interpolation or concatenation to format it in the desired way.

Here's the most concise way to do it in JavaScript:

index.tsx
const dateStr = '05.11.2022';
const formattedDate = `${new Date(dateStr).getDate()}/${new Date(dateStr).getMonth() + 1}/${new Date(dateStr).getFullYear()}`;
console.log(formattedDate); // Output: 5/11/2022
206 chars
4 lines

Alternatively, you can write it in a more explicit manner by storing the parsed date in a variable first:

index.tsx
const dateStr = '05.11.2022';
const parsedDate = new Date(dateStr);
const formattedDate = `${parsedDate.getDate()}/${parsedDate.getMonth() + 1}/${parsedDate.getFullYear()}`;
console.log(formattedDate); // Output: 5/11/2022
223 chars
5 lines

gistlibby LogSnag