The Wayback Machine - https://web.archive.org/web/20241009074608/https://www.geeksforgeeks.org/javascript-date-getmonth-method/
Open In App

JavaScript Date getMonth() Method

Last Updated : 12 Jul, 2024
Summarize
Comments
Improve
Suggest changes
Like Article
Like
Save
Share
Report
News Follow

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.

Example 1: Extracting Month from a Date Object in JavaScript

The code creates a Date object for October 15, 1996. It then uses getMonth() to extract the month, which is zero-indexed (October corresponds to 9), and prints it to the console.

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: Invalid Date Input in Date getMonth() Method

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: Providing no parameter in Date getMonth() Method

If nothing as a parameter is given, it returns the current month. It is returning 2 as March is the third month and months are zero-indexed in JavaScript.

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:

JavaScript Date getMonth() Method- FAQs

What does the Date.prototype.getMonth() method do in JavaScript?

The getMonth() method returns the month for the specified date according to local time, as an integer between 0 and 11. January is represented by 0, February by 1, and so on up to December, which is represented by 11.

Can getMonth() be used to determine the quarter of the year?

Yes, you can use getMonth() to determine the quarter of the year by checking the returned value. For example, months 0-2 correspond to Q1, 3-5 to Q2, and so on.

Can getMonth() be used to format dates?

While getMonth() itself does not format dates, it can be used in conjunction with other methods to format dates based on the month.

Can getMonth() be used to set the month?

No, getMonth() is a getter method and cannot be used to set the month. Use setMonth() to change the month of a Date object.

What are some common use cases for getMonth()?

Common use cases include:

  • Displaying the current month in a user interface.
  • Performing operations based on the current month, such as seasonal calculations.
  • Validating dates within a specific month.

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


Previous Article
Next Article

Similar Reads

How to validate if input date (start date) in input field must be before a given date (end date) using express-validator ?
In HTML forms, we often required validation of different types. Validate existing email, validate password length, validate confirm password, validate to allow only integer inputs, these are some examples of validation. In certain cases, we want the user to type a date that must come before some given date (Ex: 'start date' must come before 'end da
5 min read
How to check the input date is equal to today's date or not using JavaScript ?
Given a date object and the task is to determine whether the given date is the same as today's date or not with the help of JavaScript. There are two well-famous approaches are discussed here. Approach 1: Get the input date from user (var inpDate) and the today's date by new Date(). Now, use .setHours() method on both dates by passing parameters of
3 min read
How to convert UTC date time into local date time using JavaScript ?
Given an UTC date and the task is to convert UTC date time into local date-time using JavaScript toLocaleString() function. Syntax: var theDate = new Date(Date.parse('DATE_IN_UTC')) theDate.toLocaleString() Example 1: This example converts UTC date time into local date time using JavaScript. C/C++ Code <body> <h1 style="color:green;
1 min read
Difference Between new Date() and Date.now() in JavaScript
In JavaScript, handling dates and times is essential for various applications from displaying the current date to performing complex calculations involving time differences. Two commonly used methods for working with the dates are new Date() and Date.now(). Despite dealing with the time they serve different purposes and have distinct behaviors. The
3 min read
Node.js date-and-time Date.isLeapYear() Method
The date-and-time.Date.isLeapYear() is a minimalist collection of functions for manipulating JS date and time module which is used to check if the given year is a leap year or not. Required Module: Install the module by npm or used it locally. By using npm: npm install date-and-time --save By using CDN link: <script src="/path/to/date-and-time.m
2 min read
Node.js date-and-time Date.transform() Method
The date-and-time.Date.transform() method is used to transform date and time from the existing string format to new string format. Required Module: Install the module by npm or used it locally. By using npm:npm install date-and-time --saveBy using CDN link:<script src="/path/to/date-and-time.min.js"></script> Syntax: const transform(dat
2 min read
Node.js date-and-time Date.locale() Method
The date-and-time.Date.locale() method is used to get or switch the locale from one into another language. Required Module: Install the module by npm or used it locally. By using npm: npm install date-and-time --save By using CDN link: <script src="/path/to/date-and-time.min.js"></script> Syntax: locale([code 1="locale" language="[,"][/
1 min read
Node.js date-and-time Date.isSameDay() Method
The date-and-time.Date.isSameDay() method is used to check if the given dates are the same or not. Required Module: Install the module by npm or used it locally. By using npm:npm install date-and-time --saveBy using CDN link:<script src="/path/to/date-and-time.min.js"></script> Syntax: isSameDay(date1, date2) Parameters: This method tak
2 min read
Node.js date-and-time Date.addSeconds() Method
The date-and-time.Date.addSeconds() method is used to add the extra Seconds to the existing date and time. Required Module: Install the module by npm or used it locally. By using npm:npm install date-and-time --saveBy using CDN link:<script src="/path/to/date-and-time.min.js"></script>Syntax: const addSeconds(dateObj, seconds)Parameters
2 min read
Node.js date-and-time Date.addMinutes() Method
The date-and-time.Date.addMinutes() method is used to add the extra Minutes to the existing date and time. Required Module: Install the module by npm or used it locally. By using npm:npm install date-and-time --saveBy using CDN link:<script src="/path/to/date-and-time.min.js"></script>Syntax: const addMinutes(dateObj, minutes)Parameters
2 min read
Node.js date-and-time Date.addMilliseconds() Method
The date-and-time Date.addMilliseconds() method is used to add the extra Milliseconds to the existing date and time. Required Module: Install the module by npm or used it locally. By using npm:npm install date-and-time --saveBy using CDN link:<script src="/path/to/date-and-time.min.js"></script>Syntax: const addMilliseconds(dateObj, sec
2 min read
Node.js date-and-time Date.subtract() Method
The date-and-time Date.subtract() method is used to subtract the two date objects. Required Module: Install the module by npm or used it locally. By using npm.npm install date-and-time --saveBy using CDN link.<script src="/path/to/date-and-time.min.js"></script>Syntax: const subtract(date1, date2)Parameters: This method takes the follow
2 min read
How to Restrict User to Enter Date Manually in Date Field using AngularJS ?
In this article, we will see how to restrict the user to enter the date manually in the date field using AngularJS, along with understanding its implementation through the example. Approach: First, we need to write the code for input, and we need to give its type as the date in the HTML file.Then in-order to restrict the user to enter the date manu
2 min read
React.js Blueprint Date Input Date Formatting
Blueprint is a React-based UI toolkit for the web. This library is very optimized and popular for building interfaces that are complex and data-dense for desktop applications. In this article, we'll discuss React.js Blueprint Date Input Date formatting. The Date Input component is a type of InputGroup that allows the users to pick the date as per c
3 min read
How to set minimum and maximum date in HTML date picker
In HTML, validating and setting the minimum and maximum dates can be done using various approaches. This is useful in the real-time applications of booking, event scheduling, etc. Table of Content Using min and max AttributesUsing JavaScript to Set min and max DatesUsing min and max AttributesIn this approach, we are using the min and max attribute
2 min read
JavaScript Date Comparison: How to Compare Dates in JavaScript?
JavaScript Date Comparison is a common task and used in many applications. Like - building scheduling features, filtering data by date, or just checking if one date is before or after another. However, working with dates can be tricky due to different formats and time zones. In this article, we will explore various ways to compare dates in JavaScri
4 min read
JavaScript Date getMinutes() Method
JavaScript Date getMinutes() Method is used to fetch the minutes from the given Date object.Syntax: DateObj.getMinutes() Parameter: This function does not accept any parameter. Return Value: It returns the minutes for the given Date Object. Minutes is an integer value ranging from 0 to 59.: The below examples illustrate JavaScript Date getMinutes()
2 min read
JavaScript Date getMilliseconds() Method
The date.getMilliseconds() method is used to fetch the milliseconds from a given Date object. Syntax: DateObj.getMilliseconds() Parameter: This function does not accept any parameter. Return values: It returns the millisecond for the given date object. Milliseconds is an integer value ranging from 0 to 999. Below is the example of Date.getMilliseco
3 min read
JavaScript Date getUTCDate() Method
The date.getUTCDate() method is used to fetch the date of a month according to universal time from a given Date object ranging from 1 to 31. Syntax: DateObj.getUTCDate(); Parameter: This method does not accept any parameter. It is just used along with a Date Object from which we want to fetch the date of the month according to universal time. Retur
2 min read
JavaScript Date getUTCDay() Method
The date.getUTCDay() method is used to fetch the day of a week according to universal time from a given Date object (ranging from 0 to 6, where 0 represents Sunday). Syntax: DateObj.getUTCDay(); Parameter: This method does not accept any parameter. It is just used along with a Date Object from which we want to fetch the date of the month according
2 min read
JavaScript Date getUTCFullYear() Method
The date.getUTCFullYear() method is used to fetch the year according to universal time from a given Date object. Syntax: DateObj.getUTCFullYear(); Parameter: This method does not take any parameter. It is just used along with a Date Object from which we want to fetch the year according to universal time. Return Values: It returns the year for the g
2 min read
JavaScript Date getUTCHours() Method
The date.getUTCHours() method is used to fetch the hours according to universal time from a given Date object (returns a value ranging from 0 to 23 ). Syntax: DateObj.getUTCHours(); Parameters: This method does not accept any parameter. It is just used along with a Date Object from which we want to fetch hours according to universal time. Return Va
3 min read
JavaScript Date getUTCMilliseconds() Method
The date.getUTCMilliseconds() method is used to fetch the millisecond according to universal time from a given Date object (returns a value ranging from 0 to 999). Syntax: DateObj.getUTCMilliseconds(); Parameter: This method does not accept any parameter. It is just used along with a Date Object from which we want to fetch milliseconds according to
3 min read
JavaScript Date getUTCSeconds() Method
The date.getUTCSeconds() method is used to fetch the second according to universal time from a given Date object (returns a value ranging between 0 to 59). Syntax: DateObj.getUTCSeconds(); Parameter: This method does not take any parameter. It is just used along with a Date Object from which we want to fetch the day of the week. Return Values: It r
4 min read
JavaScript Date getUTCMinutes() Method
The Javascript date.getUTCMinutes() method is used to fetch the minutes according to universal time from a given Date object (returns a value ranging from 0 to 59). Syntax: DateObj.getUTCMinutes(); Parameters: This method does not accept any parameter. It is just used along with a Date Object from which we want to fetch minutes. Return Values: It r
3 min read
JavaScript Date getUTCMonth() Method
The Javascript date.getUTCMonth() method is used to fetch the month according to universal time from a given Date object. Syntax: DateObj.getUTCMonth(); Parameters: This method does not accept any parameter. It is just used along with a Date Object from which we want to fetch months. Return Values: It returns the months for the given date object ac
3 min read
JavaScript Date setFullYear() Method
JavaScript date.setFullYear() method is used to set year into a date object which is created using Date() constructor. Syntax: DateObj.setFullYear(year_Value) Parameter: This method accepts a single parameter as mentioned above and described below: year_Value: This parameter holds the value of year which is used to set in the Date() constructor. Re
3 min read
JavaScript Date setSeconds() Method
The date.setSeconds() method is used to set seconds into a Date object which is created using Date() constructor. Syntax: DateObj.setSeconds(seconds_Value) Parameter: This method accepts a single parameter as mentioned above and described below: seconds_Value: This parameter holds the value of second which is used to set in the Date() constructor.
4 min read
JavaScript Date setMinutes() Method
JavaScript date.setMinutes() method is used to set minutes into a Date object which is created using the Date() constructor. Syntax: DateObj.setMinutes(Minutes_Value) Parameter: This method accepts a single parameter as mentioned above and described below: minutes_Value: This parameter holds the value of minutes which is used to set in the Date() c
4 min read
JavaScript Date setMonth() Method
The date.setMonth() method is used to set month into a date object which is created using the Date() constructor. Syntax: DateObj.setMonth(month_Value); Parameter: This method accepts a single parameter as mentioned above and described below: month_Value: This parameter holds the value of the month that we want to set in the date created using the
4 min read