format date to 5/11/2022 in javascript

You can use toLocaleDateString() method to format a date in JavaScript.

const date = new Date('11/05/2022');
const formattedDate = date.toLocaleDateString('en-US');
console.log(formattedDate); // Output: 5/11/2022
142 chars
4 lines

Here, toLocaleDateString() method takes the locale as the first argument, which specifies the format of the output string. The default locale is the user's locale, which may not be in the desired format. Therefore in this example, the en-US locale is used to format the date string.

gistlibby LogSnag