JavaScript Date getMonth() method - JavaScript Tutorial

JavaScript Date getMonth() method

BLUF: This tutorial on JavaScript Date getMonth() method provides an in-depth look at JavaScript's core features. It includes practical examples and code snippets to help you master modern JS development.
Key Discovery: JavaScript Date getMonth() method

Understanding JavaScript Date getMonth() method is crucial for building dynamic, interactive web applications. Explore the examples below to see it in action.

The getMonth function in JavaScript retrieves the integer corresponding to the month of a given date based on local time. It is important to note that the value returned by the getMonth function begins at 0, where 0 signifies January.

Syntax

The getMonth function is expressed using the subsequent syntax:

Example

dateObj.getMonth()

Return

An integer within the range of 0 to 11 that indicates the month of the given date.

JavaScript Date getMonth method example

In this section, we will explore the getMonth method by examining several examples.

Example 1

Let's see an example to print current month.

Example

<script>

var date=new Date();

document.writeln(date.getMonth()+1);

</script>

Output:

Example 2

Let's examine an example that demonstrates how to extract and display the month from a specified date.

Example

<script>

var date=new Date("August 15, 1947 20:22:10");

document.writeln(date.getMonth()+1)

</script>

Output:

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience