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 = year + "-" + month + "-" + day; document.write(formattedDate); // Prints: 2022-05-04 </script> </body> </html>