To extract the day, month, and year from a JavaScript Date object, you can use the built-in methods getDate()
, getMonth()
, and getFullYear()
respectively. Here's an example:
const date = new Date(); // create a Date object with the current date
const day = date.getDate(); // get the day (1-31)
const month = date.getMonth() + 1; // get the month (0-11, so add 1 to convert to 1-12)
const year = date.getFullYear(); // get the four-digit year
console.log(day, month, year); // print the extracted values
This code will output the current day, month, and year. Keep in mind that getMonth()
returns a zero-based index for months (0 = January, 1 = February, etc.), so you need to add 1 to get the correct month value.