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