In JavaScript, you can set the month through the setMonth method. The syntax is "dateObject.setMonth(month,day)". The parameter month is a numerical value representing the month, and the value is between 0 and 11. .
The operating environment of this tutorial: windows7 system, javascript version 1.8.5, Dell G3 computer.
setMonth() method is used to set the month.
Syntax
dateObject.setMonth(month,day)
Parameters
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.
[Recommended learning: js basic tutorial]
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>
Output:
Sun Jan 31 2021 11:47:22 GMT+0800 (中国标准时间)
Example 2
In this example, we will set the month field of object d to 0 (January) and the day field to 20 through the setMonth() method:
<script type="text/javascript"> var d=new Date() d.setMonth(0,20) document.write(d) </script>
Output :
Wed Jan 20 2021 11:47:22 GMT+0800 (中国标准时间)
The above is the detailed content of How to set the month in javascript. For more information, please follow other related articles on the PHP Chinese website!