Today's date (default): March 12, 2025
Today's date (short): 03/12/25
A specific date: March 15, 2024
Another date with time: March 15, 2024 10:30 am
Invalid Date: Invalid date
Two years ago: March 12, 2023
Explanation: Functions group reusable code. This formatDate()
function takes a timestamp (or a date string) and an optional format string and returns a formatted date. It handles invalid date input and allows you to easily change the date format throughout your website by modifying the function. This is much more efficient and maintainable than writing the date formatting code repeatedly.