format string 05.11.2022 into date 11/5/2022 in moment.js in javascript

To format a string in "dd/mm/yyyy" format using moment.js, you can use the moment function to parse and create a moment object from the string, and then use the format() function to output the formatted string with the desired format. Here's an example:

index.tsx
const dateString = '05.11.2022';
const date = moment(dateString, 'DD.MM.YYYY');
const formattedDate = date.format('MM/DD/YYYY');
console.log(formattedDate); // Outputs: 11/05/2022
180 chars
5 lines

In the code above, the dateString is the input string to be formatted, and the date variable is a moment object created from the string using the moment function with a format string of 'DD.MM.YYYY'.

Then, the formattedDate variable is created by calling the format() function on the date object with the desired format string of 'MM/DD/YYYY', where 'MM' represents the month, 'DD' represents the day, and 'YYYY' represents the year.

Finally, we output the formattedDate string to the console using console.log().

gistlibby LogSnag