To get the current `date`, `month`, and `year` in JavaScript, you can use the `Date` object and its methods.
Here's an example of how you can get the current date, month, and year:
const today = new Date();
const date = today.getDate();
const month = today.getMonth(); // months are 0-based, so add 1 to get the correct month number
const year = today.getFullYear();
console.log(`Date: ${date}`);
console.log(`Month: ${month + 1}`);
console.log(`Year: ${year}`);
The `getDate()` method returns the day of the month (from 1 to 31) for the specified date, the `getMonth()` method returns the month (from 0 to 11) for the specified date, and the `getFullYear()` method returns the year (four digits) for the specified date.
Note: `getMonth()` method returns a 0-based value, so you'll need to add 1 to get the correct month number (for example, 0 for January, 1 for February, etc.).
You can also use the `toLocaleDateString()` method to get the current date in a formatted string, like this:
const today = new Date();
const dateString = today.toLocaleDateString();
console.log(dateString); // e.g. "12/20/2022" (depending on your locale)
Comments (0)