JavaScript Date getDate() Method
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.
Syntax:
DateObj.getDate()
Parameter: This method does not take any parameters.
Return Value: Returns the date of the month for the given date. The date of the month is an integer value ranging from 1 to 31.
Note: The DateObj is a valid Date object created using the Date() constructor from which we want to fetch the date.
JavaScript Date getDate() Method Examples
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.
// Here a date has been assigned
// while creating Date object
let dateobj = new Date('October 13, 1996 05:35:32');
// date of the month from above Date Object is
// being extracted using getDate()
let B = dateobj.getDate();
// Printing date of the month
console.log(B);
Output
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.
// Here a date has been assigned
// while creating Date object
let dateobj = new Date('October 33, 1996 05:35:32');
// date of the month given above date object
// is being extracted using getDate().
let B = dateobj.getDate();
// Printing date of the month.
console.log(B);
Output
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.
// Here a date has been assigned
// while creating Date object
let dateobj = new Date('October 1996 05:35:32');
// date of the month from above date object
// is extracted using getDate()
let B = dateobj.getDate();
// Printing date of the month
console.log(B);
Output
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.
// Creating Date Object
let dateobj = new Date();
// date of the month from above object
// is being extracted using getDate().
let B = dateobj.getDate();
// Printing current date
console.log(B);
Output
14
Supported Browsers:
- Google Chrome 1 and above
- Microsoft Edge 12 and above
- Mozilla Firefox 1 and above
- Internet Explorer 3 and above
- Opera 3 and above
- Safari 1 and above
We have a complete list of Javascript Date Objects, to check those please go through this Javascript Date Object Complete reference article.