Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Format Date as YYYY-MM-DD</title> </head> <body> <script> // Create a date object from a date string var date = new Date("Wed, 04 May 2022"); // Get year, month, and day part from the date var year = date.toLocaleString("default", { year: "numeric" }); var month = date.toLocaleString("default", { month: "2-digit" }); var day = date.toLocaleString("default", { day: "2-digit" }); // Generate yyyy-mm-dd date string var formattedDate = day + "-" + month + "-" + year; document.write(formattedDate); // Prints: 04-05-2022 </script> </body> </html>