Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Format a Date String</title> </head> <body> <script> // Create a date object from a date string var date = new Date("2021-10-06"); // yyyy-mm-dd // Get year, month, and day part from the date var year = date.toLocaleString("default", { year: "numeric" }); var month = date.toLocaleString("default", { month: "short" }); var day = date.toLocaleString("default", { day: "2-digit" }); // Generate custom date string var formattedDate = day + "-" + month + "-" + year; document.write(formattedDate); // Prints: 06-Oct-2021 </script> </body> </html>