![]() |
VOOZH | about |
The JavaScript getDate() method returns the day of the month (from 1 to 31) for a specified date according to local time.
It's used with the Date object to extract and work with the day component of a date.
DateObj.getDate()Note: The DateObj is a valid Date object created using the Date() constructor from which we want to fetch the date.
Example 1: Extracting Day of the Month Using JavaScript Date getDate() Method
The code initializes a Date object with the date October 13, 1996, and then uses the getDate() method to extract the day of the month, which is 13. This value is printed to the console.
13
Example 2: Extracting Day of the Month with Invalid Date Using JavaScript Date getDate() Method
The code initializes a date object with an invalid date string. Then, it attempts to extract the day of the month using getDate(). Since the date is invalid, it returns NaN.
NaN
Example 3: Extracting Day from Year-Only Date
If date of the month is not given, by default it returns 1. It is an exceptional case.
1
Example 4: Current Day Extraction
A Date object is created without parameters, representing the current date. getDate() extracts the day of the month from this object, which is then printed.