Home  >  Article  >  Web Front-end  >  The JavaScript method getUTCMonth() returns a number representing the month (UTC)

The JavaScript method getUTCMonth() returns a number representing the month (UTC)

黄舟
黄舟Original
2017-11-06 13:20:131551browse

Definition and Usage

The getUTCMonth() method returns a number representing the month (according to Universal Time UTC).

Syntax

dateObject.getUTCMonth()

Return value

Return dateObject The month expressed in universal time, the value is between 0 (January) ~ 11 (December) An integer.

It should be noted that the Date object uses 1 to represent the first day of the month, instead of using 0 to represent the first month of the year like the month field.

Tips and Notes:

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

Tip: For more information about Universal Coordinated Time (UTC), please refer to Baidu Encyclopedia.

Example

Example 1

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

<script type="text/javascript">

var d=new Date()

document.write(d.getUTCMonth())

</script>

Output:

10

Example 2

Now, we will create an array so that our example above can output the name of the month instead of just 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.getUTCMonth()])

</script>

Output:

The month is November

The javascript Date.getUTCMonth() method returns the month on the specified date according to universal time. The value returned by getUTCMonth is an integer between 0 and 11 corresponding to the month. 0 represents January, 1 represents February, 2 represents March, and so on.

Example:

The following example prints the month part of the current time variablehrs.

<html>
<head>
<title>JavaScript getUTCMonth Method</title>
</head>
<body>
<script type="text/javascript">
  var dt = new Date();
  document.write("getUTCMonth() : " + dt.getUTCMonth() ); 
</script>
</body>
</html>

This will produce the following results:

getUTCMonth() : 7


The above is the detailed content of The JavaScript method getUTCMonth() returns a number representing the month (UTC). For more information, please follow other related articles on the PHP Chinese website!

Statement:
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