What is getDate() in JavaScript?

The getDate() method is one of the date methods available in javaScript. It is used to get the day of the month for the specified date.

Syntax

getDate()

Return value

The getDate() method returns a number, from 1 to 31, which represents the days in a month. The value returned depends on the value stored in the Date object. It could either be the current date or user-specified.

Usage

The new Date() constructor is used with the getDate() method to get the day of the month from the specified date.

Examples

The following are examples of passing a date to the new Date() constructor to get the day using the getDate() method.

// EXAMPLE 1
console.log("Example 1: Date as a string:");
const dayInMonth = new Date('Jul 11 2011');
const day1 = dayInMonth.getDate();
console.log(day1)
//EXAMPLE 2
console.log("Example 2: Date as ISOString");
const dayInMonthIsos = new Date('2020-06-13T18:30:00.000Z');
const dayIsos = dayInMonthIsos.getDate();
console.log(dayIsos);
//EXAMPLE 3
console.log("EXAMPLE 3: Date as UTCString");
const dayInMonthUtc = new Date('Sat, 13 Jun 2020 18:30:00 GMT');
const dayUtc = dayInMonthUtc.getDate();
console.log(dayUtc);
//EXAMPLE 4
console.log("Example 4: Date as LocaleDateString");
const dayInMonthLocale = new Date('6/14/2020');
const dayLocale = dayInMonthLocale.getDate();
console.log(dayLocale);
//EXAMPLE 5
console.log("Example 5: Date as parameters");
const dayInMonthPara = new Date(12, 13, 01);
const dayPara = dayInMonthPara.getDate();
console.log(dayPara);
//EXAMPLE 6
console.log("Example 6: Date without parameters");
const dayInNoPara = new Date();
const dayNoPara = dayInNoPara.getDate();
console.log(dayNoPara);

From the examples given, we can see the different date formats that work as either strings or parameters with the getDate() method.

Without parameters, getDate() is used to get the day of the current date. This is demonstrated in the last example.

Note:

The getDate() method never works in isolation. It always works with the date constructor new Date().