--- title: Date.prototype.setMonth() slug: Web/JavaScript/Reference/Global_Objects/Date/setMonth translation_of: Web/JavaScript/Reference/Global_Objects/Date/setMonth --- <div>{{JSRef("Global_Objects", "Date")}}</div> <p><code><strong>setMonth()</strong></code> 方法根据本地时间为一个设置年份的日期对象设置月份。</p> <div>{{EmbedInteractiveExample("pages/js/date-setmonth.html")}}</div> <h2 id="Syntax">语法</h2> <pre class="syntaxbox"><var>dateObj</var>.setMonth(<em>monthValue</em>[, <em>dayValue</em>])</pre> <h3 id="Versions_prior_to_JavaScript_1.3">JavaScript 1.3版本之前</h3> <pre class="syntaxbox"><var>dateObj</var>.setMonth(<em>monthValue</em>)</pre> <h3 id="Parameter">参数</h3> <dl> <dt><code>monthValue</code></dt> <dd>介于 0 到 11 之间的整数(表示一月到十二月)。</dd> <dt><code>dayValue</code></dt> <dd>从 1 到 31 之间的整数,表示月份中的第几天。0为上个月最后一天</dd> <dt>返回值!</dt> <dd>基于 1 January 1970 00:00:00 UTC 开始计算的毫秒数</dd> </dl> <h2 id="Description">描述</h2> <p>如果不指定 <code>dayValue</code> 参数,就会使用 {{jsxref("Date.getDate", "getDate")}} 方法的返回值。</p> <p>如果有一个指定的参数超出了合理范围,<code>setMonth</code> 会相应地更新日期对象中的日期信息。例如,为 <code>monthValue</code> 指定 15,则年份会加 1,月份将会使用 3。</p> <h2 id="Examples">例子</h2> <h3 id="Example_Using_setMonth">例子:使用<code>setMonth方法</code></h3> <pre class="brush: js">var theBigDay = new Date(); theBigDay.setMonth(6); </pre> <h2 id="规范">规范</h2> <table class="standard-table"> <tbody> <tr> <th scope="col">规范版本</th> <th scope="col">规范状态</th> <th scope="col">注解</th> </tr> <tr> <td>ECMAScript 1st Edition. Implemented in JavaScript 1.0</td> <td>Standard</td> <td>Initial definition.</td> </tr> <tr> <td>{{SpecName('ES5.1', '#sec-15.9.5.38', 'Date.prototype.setMonth')}}</td> <td>{{Spec2('ES5.1')}}</td> <td></td> </tr> <tr> <td>{{SpecName('ES6', '#sec-date.prototype.setmonth', 'Date.prototype.setMonth')}}</td> <td>{{Spec2('ES6')}}</td> <td></td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <p>{{Compat("javascript.builtins.Date.setMonth")}}</p> <h2 id="See_Also">相关链接</h2> <ul> <li>{{jsxref("Date.prototype.getMonth()")}}</li> <li>{{jsxref("Date.prototype.setUTCMonth()")}}</li> </ul>