Show Output
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <title>JavaScript Format Current Date String</title> </head> <body> <script> // Create a date object var today = new Date() var year = today.getFullYear(); /* Add 1 to the returned month number, as count for months starts at 0 (January) and ends at 11 (December). Also place 0 before single digit month and day number with padStart() method */ var month = String(today.getMonth() + 1).padStart(2, '0'); var day = String(today.getDate()).padStart(2, '0'); // Generate current date string in UK date format (day-month-year) var ukDate = [day, month, year].join('/'); document.write(ukDate + "<br>"); // Generate current date string in US date format (month-day-year) var usDate = [month, day, year].join('/'); document.write(usDate); </script> </body> </html>