format date 01.11.2022 to 11/1/2022 in javascript

You can use the replace() method to format the date string. Here's an example:

index.tsx
let dateString = '01.11.2022';
let formattedDate = dateString.replace(/^(\d{2}).(\d{2}).(\d{4})$/, '$2/$1/$3');
console.log(formattedDate); // 11/01/2022
154 chars
4 lines

Explanation:

  • The regular expression /^(\d{2}).(\d{2}).(\d{4})$/ matches the string in the format dd.mm.yyyy, where dd, mm, yyyy are two-digit day, month, and year values respectively.
  • The three parts of the date are captured in groups (\d{2}), (\d{2}), (\d{4}) using parentheses.
  • The replace() method replaces the matched string with the second group ($2), followed by a slash (/), then the first group ($1), another slash, and finally the third group ($3), resulting in the desired mm/dd/yyyy format.

gistlibby LogSnag