format date 11-11-2020 to dd.mm.yyyy in javascript

You can use the string.replace method along with a regular expression to reformat the date string. Here's an example code snippet that shows how to do this:

index.tsx
const inputDate = '11-11-2020';
const formattedDate = inputDate.replace(/^(\d{2})-(\d{2})-(\d{4})$/, '$1.$2.$3');

console.log(formattedDate); // Output: "11.11.2020"
167 chars
5 lines

In the above code, we're using the replace method to replace the hyphens with dots in the date string. The regular expression ^(\d{2})-(\d{2})-(\d{4})$ matches the date string in the format of dd-mm-yyyy and captures the day, month, and year as separate groups. The replacement string '$1.$2.$3' then puts these groups back together separated by dots, resulting in the desired date format of dd.mm.yyyy.

gistlibby LogSnag