Home > Web Front-end > JS Tutorial > body text

JavaScript method getMonth() returns the month (0 ~ 11) from the Date object

黄舟
Release: 2017-11-04 14:25:41
Original
6946 people have browsed it

Definition and usage

getMonth() method returns the number representing the month.

Syntax

dateObject.getMonth()
Copy after login

Return value

The month field of dateObject, using local time. The return value is an integer between 0 (January) and 11 (December).

Tips and Comments:

Comments: This method is always used in conjunction with a Date object.

Example

Example 1

In this example, we will get the current date and output it:

<script type="text/javascript">

var d=new Date()

document.write(d.getMonth())

</script>
Copy after login

Output:

10
Copy after login

Example 2

Now, we will create an array to output the name of the month instead of a number:

<script type="text/javascript">

var d=new Date()

var month=new Array(12)
month[0]="January"
month[1]="February"
month[2]="March"
month[3]="April"
month[4]="May"
month[5]="June"
month[6]="July"
month[7]="August"
month[8]="September"
month[9]="October"
month[10]="November"
month[11]="December"

document.write("The month is " + month[d.getMonth()])

</script>
Copy after login

Output:

The month is November
Copy after login

getMonth() The return value of the function is of type Number and returns the month value of the current Date object. The value is between [0, 11].

Among them, 0 ~ 11 represent January to December respectively.

Example & Description

// 定义一个当前时间的Date对象(2014-08-07)
var date = new Date();
document.writeln( date.getMonth() ); // 7

// 定义一个"2002-06-30 12:11:59 230"的Date对象
var date2 = new Date(2002, 5, 30, 12, 11, 59, 230);
document.writeln( date2.getMonth() ); // 5

// 定义一个"2009-01-18"的Date对象
var date3 = new Date(2009, 0, 18);
document.writeln( date3.getMonth() ); // 0
Copy after login


The above is the detailed content of JavaScript method getMonth() returns the month (0 ~ 11) from the Date object. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template