How to create a JavaScript Date object?

To create a JavaScript Date object, you can use one of the following methods:

  1. Using the constructor:

    let date = new Date(); // creates a Date object with the current date and time
  2. Using a specific date and time:

    let date = new Date(year, monthIndex, day, hour, minute, second, millisecond); // year: the full year (e.g., 2022) // monthIndex: month index starting from 0 (e.g., 0 for January, 1 for February) // day: day of the month (1-31) // hour: hours (0-23) // minute: minutes (0-59) // second: seconds (0-59) // millisecond: milliseconds (0-999)
  3. Using a date string:

    let date = new Date('Month Day, Year Time'); // Month: a full month name or a three-letter abbreviation (e.g., January, Jan) // Day: day of the month (1-31) // Year: the full year (e.g., 2022) // Time: time in the format HH:MM:SS (e.g., 10:30:00)

Note: JavaScript counts months starting from 0 (0 for January, 1 for February, etc.), and days starting from 1.