Thursday, December 26, 2024
Google search engine
HomeLanguagesJavascriptJavaScript Date getMonth() Method

JavaScript Date getMonth() Method

The date.getMonth() method is used to fetch the month(0 to 11) from the given Date object (0 represents the first month of the year).

Syntax:

DateObj.getMonth()

Parameter: This function does not accept any parameter.

Return Value: It returns the Month for the given Date object. The month is an integer value ranging from 0 to 11. Zero (0) means January, 1 means February, and so on till 11 means December.

The below examples illustrate the JavaScript Date getMonth() Method:

Example 1: In this example, we will use the Date getMonth() method.

javascript




// Creating a Date Object
let DateObj = new Date('October 15, 1996 05:35:32');
 
// Month from above Date Object is
// Being extracted using getMonth()
let months = DateObj.getMonth();
 
// Printing month.
console.log(months);


Output:

9

Example 2: Here the date of the month should lie between 1 to 31 because no date can have a month greater than 31. That is why it returns NaN i.e, Not a Number if the month in the Date object is greater than 31.

javascript




// Creating a Date Object
let DateObj = new Date('October 33, 1996 05:35:32');
 
// Month from above Date Object is being
// Extracted using getMonth()
let months = DateObj.getMonth();
 
// Printing month.
console.log(months);


Output:

NaN

Example 3: If the month is not given, it returns zero (0).

javascript




// Creating a Date Object
let DateObj = new Date('1996 05:35:32');
 
// Month from above Date Object is being
// Extracted using getMonth()
let months = DateObj.getMonth();
 
// Printing month.
console.log(months);


Output:

0

Example 4: If nothing as a parameter is given, it returns the current month.

javascript




// Creating a Date Object
let DateObj = new Date();
 
// Month from above Date Object is being
// Extracted using getMonth()
let months = DateObj.getMonth();
 
// Printing month.
console.log(months);


Output:

2

Supported Browsers: The browsers supported by the JavaScript Date getMonth() method are listed below:

  • Google Chrome 1 and above
  • Edge 12 and above
  • Firefox 1 and above
  • Internet Explorer 4 and above
  • Opera 3 and above
  • Safari 1 and above

We have a complete list of Javascript Javascript Date methods, to check those please go through the Javascript Date Object Complete Reference article.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments