JavaScript Date getDate() Method



In JavaScript, the Date.getDate() method is used to retrieve the "day of month" from a date object. This method does not accept any parameters instead; it returns the day of the month as a numeric value between 1 and 31, representing the current day according to the local time zone. If the date object is invalid, it returns NaN (Not-a-Number).

Syntax

Following is the syntax of JavaScript Date getDate() method −

 getDate(); 

This method does not accept any parameters.

Return Value

This method returns a numeric value representing the day of the month (1-31) for the given Date object.

Example 1

In the following example, we are demonstrating the basic usage of JavaScript Date getDate() method −

 <html> <body> <script> const CurrentDate = new Date(); const DayOfMonth = CurrentDate.getDate(); document.write(DayOfMonth); </script> </body> </html> 

Output

The above program returns the day of the month of the current date.

Example 2

In this example, we are retrieving the day of the month for a specific date "December 25, 2023" −

 <html> <body> <script> const SpecificDate = new Date('2023-12-25'); const DayOfMonth = SpecificDate.getDate(); document.write(DayOfMonth); </script> </body> </html> 

Output

After executing, this program returns 25 as output.

Example 3

Here, we are using the setDate() method to add 10 days to the current date −

 <html> <body> <script> const currentDate = new Date(); currentDate.setDate(currentDate.getDate() + 10); const result = currentDate.getDate(); document.write(result); </script> </body> </html> 

Output

As we can see the output, 10 days has been added to the current date.

Advertisements