JavaScript(JS) JS display current date

In JavaScript, you can display the current date using the Date object. Here's an example:

const currentDate = new Date();

console.log(currentDate);
S‮www:ecruo‬.theitroad.com

In the above example, we create a new Date object without any arguments, which sets the date to the current date and time. We then log the Date object to the console, which displays the date and time in a string format.

If you want to display the date in a specific format, you can use the various methods of the Date object to get the individual components of the date (year, month, day, etc.) and concatenate them into a formatted string. Here's an example of displaying the current date in the format YYYY-MM-DD:

const currentDate = new Date();

const year = currentDate.getFullYear();
const month = String(currentDate.getMonth() + 1).padStart(2, '0');
const day = String(currentDate.getDate()).padStart(2, '0');

const formattedDate = `${year}-${month}-${day}`;

console.log(formattedDate);

In this example, we create a new Date object and use the getFullYear(), getMonth(), and getDate() methods to get the year, month, and day components of the date. We then use the padStart() method to add leading zeros to the month and day strings if they are less than two characters long. Finally, we concatenate the year, month, and day strings into a formatted string with the format YYYY-MM-DD and log it to the console.