Home  >  Article  >  Web Front-end  >  Detailed explanation of the use of setMonth() method in JavaScript_Basic knowledge

Detailed explanation of the use of setMonth() method in JavaScript_Basic knowledge

WBOY
WBOYOriginal
2016-05-16 15:55:421300browse

The javascript Date.setMonth() method sets the specified date of each month according to local time.
Grammar

Date.setMonth(monthValue[, dayValue])

Note: Appearance and data within brackets are optional

Here are the details of the parameters:

  • monthValue: an integer from 0 to 11 (representing a month, from January to December).
  • dayValue: an integer from 1 to 31, representing a 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's latest information accordingly. 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.
Return value:

  • NA

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>

This will produce the following results:

Fri Mar 28 23:30:00 UTC+0530 2008 

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