Date.prototype.setDate()

Baseline Widely available

This feature is well established and works across many devices and browser versions. It’s been available across browsers since July 2015.

setDate() 方法根据本地时间来指定一个日期对象的天数。

尝试一下

const event = new Date("August 19, 1975 23:15:30");

event.setDate(24);

console.log(event.getDate());
// Expected output: 24

event.setDate(32);
// Only 31 days in August!

console.log(event.getDate());
// Expected output: 1

语法

dateObj.setDate(dayValue)

参数

dayValue

一个整数,表示该月的第几天。

描述

如果 dayValue 超出了月份的合理范围,setDate 将会相应地更新 Date 对象。

例如,如果为 dayValue 指定 0,那么日期就会被设置为上个月的最后一天。

如果 dayValue 被设置为负数,日期会设置为上个月最后一天往前数这个负数绝对值天数后的日期。-1 会设置为上月最后一天的前一天(译者注:例如当前为 4 月,如果 setDate(-2),则为 3 月 29 日)

示例

示例:使用setDate方法

js
var theBigDay = new Date(1962, 6, 7); // 1962-07-07
theBigDay.setDate(24); // 1962-07-24
theBigDay.setDate(32); // 1962-08-01

规范

Specification
ECMAScript® 2025 Language Specification
# sec-date.prototype.setdate

浏览器兼容性

Report problems with this compatibility data on GitHub
desktopmobileserver
Chrome
Edge
Firefox
Opera
Safari
Chrome Android
Firefox for Android
Opera Android
Safari on iOS
Samsung Internet
WebView Android
WebView on iOS
Deno
Node.js
setDate

Legend

Tip: you can click/tap on a cell for more information.

Full support
Full support

参见