--- title: Date.prototype.setDate() slug: Web/JavaScript/Reference/Global_Objects/Date/setDate translation_of: Web/JavaScript/Reference/Global_Objects/Date/setDate --- <div>{{JSRef("Global_Objects", "Date")}}</div> <p><code><strong>setDate()</strong></code> 方法根据本地时间来指定一个日期对象的天数。</p> <div>{{EmbedInteractiveExample("pages/js/date-setdate.html")}}</div> <h2 id="Syntax" name="Syntax">语法</h2> <pre class="syntaxbox"><code><var>dateObj</var>.setDate(<em>dayValue</em>)</code></pre> <h3 id="Parameters" name="Parameters">参数</h3> <dl> <dt><code>dayValue</code></dt> <dd>一个整数,表示该月的第几天。</dd> </dl> <h2 id="Description" name="Description">描述</h2> <p>如果 <code>dayValue</code> 超出了月份的合理范围,<code>setDate</code> 将会相应地更新 <code>Date</code> 对象。</p> <p>例如,如果为 <code>dayValue</code> 指定0,那么日期就会被设置为上个月的最后一天。</p> <p>如果dayValue被设置为负数,日期会设置为上个月最后一天往前数这个负数绝对值天数后的日期。-1会设置为上月最后一天的前一天(译者注:例如当前为4月,如果setDate(-2),则为3月29日)</p> <h2 id="Examples" name="Examples">例子</h2> <h3 id="Example_Using_setDate" name="Example:_Using_setDate">例子:使用<code>setDate</code>方法</h3> <pre class="brush:js">var theBigDay = new Date(1962, 6, 7); // 1962-07-07 theBigDay.setDate(24); // 1962-07-24 theBigDay.setDate(32); // 1962-08-01</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.36', 'Date.prototype.setDate')}}</td> <td>{{Spec2('ES5.1')}}</td> <td></td> </tr> <tr> <td>{{SpecName('ES6', '#sec-date.prototype.setdate', 'Date.prototype.setDate')}}</td> <td>{{Spec2('ES6')}}</td> <td></td> </tr> </tbody> </table> <h2 id="浏览器兼容性">浏览器兼容性</h2> <p class="hidden">The compatibility table in this page is generated from structured data. If you'd like to contribute to the data, please check out <a href="https://github.com/mdn/browser-compat-data">https://github.com/mdn/browser-compat-data</a> and send us a pull request.</p> <p>{{Compat("javascript.builtins.Date.setDate")}}</p> <h2 id="See_Also" name="See_Also">相关链接</h2> <ul> <li>{{jsxref("Date.prototype.getDate()")}}</li> <li>{{jsxref("Date.prototype.setUTCDate()")}}</li> </ul>