JavaScript date.getDate() method


The date.getDate() method in JavaScript returns the day of the month (from 1 to 31) for a specific date object, according to local time.

Syntax:

date.getDate();

Returns:

  • A number representing the day of the month (1-31).

Example 1: Getting the Day of the Month

const date = new Date('2024-10-22'); // October 22, 2024 const dayOfMonth = date.getDate(); console.log(dayOfMonth);

Output:

22

Explanation:

  • The getDate() method retrieves the day of the month from the Date object, which is 22 for October 22, 2024.

Example 2: Current Day of the Month

const today = new Date(); // Current date const currentDay = today.getDate(); console.log(currentDay);

Output:

(Depends on the current date)

Explanation:

  • This retrieves the current day of the month using the getDate() method. If today is October 22, it would return 22.

Summary:

  • date.getDate() returns the day of the month (from 1 to 31) for a given Date object.
  • It reflects local time and can be used to retrieve the specific day of the month from a date.