Get the current date in JavaScript

To get the current date in JavaScript, you can use the Date object. Here is a simple example:

// Create a new Date object
let today = new Date();

// Get the current date components
let year = today.getFullYear();
let month = today.getMonth() + 1; // Months are zero-based, so we add 1
let day = today.getDate();

// Format the date as desired (e.g., YYYY-MM-DD)
let formattedDate = year + '-' + (month < 10 ? '0' : '') + month + '-' + (day < 10 ? '0' : '') + day;

console.log(formattedDate);

This code will output the current date in the format YYYY-MM-DD.

Leave a Reply

Back To Top