Home > Web Front-end > JS Tutorial > JavaScript method setMonth() for setting the month

JavaScript method setMonth() for setting the month

黄舟
Release: 2017-11-07 10:15:49
Original
3668 people have browsed it

Definition and usage

The setMonth() method is used to set the month.

Syntax

dateObject.setMonth(month,day)
Copy after login
ParametersDescription
month Required. A number representing the month, between 0 (January) and 11 (December).
day

Optional. A numeric value representing the day of the month, between 1 and 31 (in local time).

This parameter is not supported until EMCAScript is standardized.

Return value

Adjusted date expressed in milliseconds. Before ECMAScript was standardized, this method returned nothing.

Tips and Notes:

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

Example

Example 1

In this example, we will set the month field of object d to 0 (January) through the setMonth() method:

<script type="text/javascript">

var d=new Date()
d.setMonth(0)
document.write(d)

</script>
Copy after login

Output:

Sat Jan 07 2017 10:14:27 GMT+0800 (中国标准时间)
Copy after login

Example 2

In this example, we will set the month field of object d to 0 (January) and the day field through the setMonth() method Set to 20:

<script type="text/javascript">

var d=new Date()
d.setMonth(0,20)
document.write(d)

</script>
Copy after login

Output:

Fri Jan 20 2017 10:14:27 GMT+0800 (中国标准时间)
Copy after login

The following are the details of the parameters:

  • monthValue: an integer from 0 to 11 (indicates a month, from January to December).

  • dayValue: An integer from 1 to 31, representing a certain day of the month.

  • msValue: A number between 0 and 999, representing milliseconds. If you specify the msValue parameter, you must also specify minutesValue and secondsValue.

If the dayValue parameter is not specified, the value returned from the getDate method is used. If the specified argument is outside the expected range, setMonth attempts to update the Date object accordingly with the latest information. For example, if you use monthValue as 15, year will be incremented by 1 (year + 1), and 3 will be used for the month value.

Example:

<html>
<head>
<title>JavaScript setMonth Method</title>
</head>
<body>
<script type="text/javascript">
  var dt = new Date( "Aug 28, 2008 23:30:00" );
  dt.setMonth( 2 );
  document.write( dt ); 
</script>
</body>
</html>
Copy after login

This will produce the following results:

Fri Mar 28 23:30:00 UTC+0530 2008
Copy after login


The above is the detailed content of JavaScript method setMonth() for setting the month. 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