format string 05.11.2022 into date 11/5/2022 in moment.js, in a not depricated way in javascript

To format a string "05.11.2022" into date "11/5/2022" in moment.js library, you can use the following code:

index.tsx
const dateString = "05.11.2022";
const formattedDate = moment(dateString, "DD.MM.YYYY").format("MM/DD/YYYY");
console.log(formattedDate); // Output: 11/05/2022
160 chars
4 lines

In the code above, moment() function is used to parse the input string dateString with format "DD.MM.YYYY". Then, format() method is used to format the date according to the desired format "MM/DD/YYYY".

Note that this method is not deprecated and will work with moment.js version 2.0+.

Also, keep in mind that in the US, dates are often written in the format "MM/DD/YYYY", while in other countries, like the UK and Australia, the format "DD/MM/YYYY" is more common. So, make sure to adjust the format string accordingly to fit your region's convention.

gistlibby LogSnag